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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值