您的位置:首页 > 其它

poj 2436 位运算+组合枚举

2015-09-10 15:47 148 查看
题意:一个农场有N只奶牛,现在发现出现了D种疾病,已知每头奶牛都得了哪几种疾病。现在要选出尽可能多的奶牛,使得这些奶牛所带的疾病种类不大于K种,问最多能选几只。

思路:疾病不多于15种,很显然暗示位运算。每一位表示一种疾病。先求出组合数C(D,K)表示的疾病,对于每种搜索N只奶牛,计数即可。一开始先把疾病数量大于K的奶牛直接删去,然后对于重复的奶牛hash一下。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <cstdlib>
using namespace std;
#define clc(s,t) memset(s,t,sizeof(s))
#define INF 0x3fffffff
int hh[1<<15],s[1005],len;
int flag[1005];
int n,m,k,res=0;
void dfs(int now,int d,int has){
if(d==m){
if(has == k){
int tmp = 0;
for(int i = 0;i<len;i++){
if((~now)&s[i])
continue;
tmp += hh[s[i]];
}
res = max(res,tmp);
}
return;
}
if(m-d<k-has)
return;
dfs(now,d+1,has);
dfs(now|(1<<d),d+1,has+1);
}
int main(){
int i,j,num,x,d;
len = 0;
scanf("%d %d %d",&n,&m,&k);
for(i = 1;i<=n;i++){
x = 0;
scanf("%d",&num);
for(j = 0;j<num;j++){
scanf("%d",&d);
x |= 1<<(d-1);
}
if(num<=k)
hh[x]++;
}
for(i = 0;i<(1<<15);i++)
if(hh[i])
s[len++] = i;
dfs(0,0,0);
printf("%d\n",res);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: