蓝桥杯--幼儿园买玩具

问题描述

蒜厂幼儿园有 n 个小朋友,每个小朋友都有自己想玩的玩具。身为幼儿园园长的你决定给幼儿园买一批玩具,由于经费有限,你只能买 m 个玩具。已知玩具商店一共卖 k 种玩具,编号为 1,2,3,…k,你让每个小朋友把想玩的玩具编号都写在了纸上。你希望满足尽可能多的小朋友的需求,请计算出最多同时能满足多少个小朋友的玩具需求。 
输入格式 
第一行,输入三个整数 n,m,k(1≤n≤100,1≤m≤k≤15),中间用空格分开。 
接下来 n 行,第 i+1(0≤i< n) 行的第一个数字 ai代表第 i 个小朋友想玩的玩具数量,接下来有 ai个数字,代表这 ai 个玩具的编号。 
输出格式 
输出一个整数,表示最多能满足多少小朋友的玩具需求。 
样例输入 
5 3 5 
2 1 4 

2 3 1 
3 2 3 4 
2 4 5 
样例输出 
3

#include<stdio.h>
#include<vector>
using namespace std;
int main(){
    int n,m,k,d;
    int max=0;
    int a[101][16]={0};
    scanf("%d %d %d",&n,&m,&k);
    for(int i=0;i<n;i++){
        scanf("%d",&d);
        a[i][0]=d;
        for(int j=1;j<=d;j++){
            scanf("%d",&a[i][j]);
        }
    }
    for(int i=0;i<(1<<k);i++){
        int tot_1=0;
        int tot=0;
    
        vector<int> v;
        for(int j=0;j<k;j++){
            if(i&(1<<j)){
                tot_1++;
                v.push_back(j+1);
                
            }
        }
        if(tot_1==m){
            for(int b=0;b<n;b++){
            int sum=0;
                    for(int c=1;c<=a[b][0];c++){
                     for(int q=0;q<v.size();q++){
                            if(a[b][c]==v[q]){
                                sum++;
                            }
                        }
                    }
                    if(sum==a[b][0]){
                        tot++;
                    }
                }
            if(max<tot){
                max=tot;
            }
        }
        
        v.clear();
    }
    printf("%d",max);
    return 0;
}
原文地址:https://www.cnblogs.com/pythonbigdata/p/8541614.html