計蒜客—購買最多的玩具(列舉)
阿新 • • 發佈:2019-02-07
幼兒園有 n 個小朋友,每個小朋友都有自己想玩的玩具。身為幼兒園園長的你決定給幼兒園買一批玩具,由於經費有限,你只能買
m 個玩具。已知玩具商店一共賣k
種玩具,編號為1,2,3,.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 0 2 3 1 3 2 3 4 2 4 5
樣例輸出
3
#include<iostream> #include<algorithm> #include<string.h> using namespace std; int child[100][20]; int mark[16]; int n,m,k,maxl=0; int w=0; int main(){ cin>>n>>m>>k; for(int i=0;i<n;i++){ cin>>child[i][0]; for(int j=1;j<=child[i][0];j++)cin>>child[i][j]; } for(int i=0;i<(1<<k);i++){ w=0; memset(mark,0,sizeof(mark)); for(int j=0;j<k;j++){ if(i&(1<<j))w++,mark[j+1]=1; if(w==m)break; } if(w!=m)continue; else{ int acount=0; for(int i=0;i<n;i++){ int flag=0; for(int j=1;child[i][j];j++){ if(mark[child[i][j]]==0) { flag=1;break;} } if(flag==0)acount++; } if(acount>maxl)maxl=acount; } } cout<<maxl<<endl; return 0; }