POJ 2436 USACO silver

题意:

总共最多有15种疾病

有n头牛,每头牛有di种疾病,分别用1-15之间的数字来表示

要求选择最多头牛 使他们的疾病种数不超过K

思路:

枚举状态,状态是当前有哪几种病,然后判断每头牛是否能选择即可

code:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn = 40000;

int cow[1005];
int tab[maxn];
int n,D,k;
void table(){
    for(int s = 0; s < (1<<15); s++){
        int cnt = 0;
        int tmp = s;
        while(tmp){
            if(tmp&1) cnt++;
            tmp = tmp>>1;
        }
        tab[s] = cnt;
    }
}
void init(){
    int di;
    memset(cow, 0, sizeof(cow));
    for(int i = 0; i < n; i++){
        scanf("%d",&di);
        int s = 0,tmp;
        while(di--){
            scanf("%d",&tmp);
            s |= (1<<(tmp-1));
        }
        cow[i] = s;
    }
}

void solve(){
    int ans = 0;
    for(int s = 0; s < (1<<D); s++){
        if(tab[s] > k) continue;
        int cnt = 0;
        for(int i = 0; i < n; i++){
            if(s == (s|cow[i]))
                cnt++;
        }
        ans = max(ans, cnt);
    }
    printf("%d\n",ans);
}
int main(){
    table();
    while(scanf("%d%d%d",&n,&D,&k) != EOF){
        init();
        solve();
    }
    return 0;
}


你可能感兴趣的:(poj)