Disease Management

Description

Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) is running through the farm. Farmer John would like to milk as many of his N (1 <= N <= 1,000) cows as possible. If the milked cows carry more than K (1 <= K <= D) different diseases among them, then the milk will be too contaminated and will have to be discarded in its entirety. Please help determine the largest number of cows FJ can milk without having to discard the milk.

Input

* Line 1: Three space-separated integers: N, D, and K

* Lines 2..N+1: Line i+1 describes the diseases of cow i with a list of 1 or more space-separated integers. The first integer, d_i, is the count of cow i's diseases; the next d_i integers enumerate the actual diseases. Of course, the list is empty if d_i is 0.

Output

* Line 1: M, the maximum number of cows which can be milked.

Sample Input

6 3 2
0
1 1
1 2
1 3
2 2 1
2 2 1

Sample Output

5

思路:需要用到位运算,D最为16,用一个int数(32位 > 16位,足够表示)的每个二进制位表示病毒的存在与否,1为存在,0不存在,这样复杂度为2^16*n(通过剪枝实际达不到这么大),可以接受。因此有两种方法解本题,(1),dfs,枚举D的k-组合。(2),通过二进制枚举D的k-组合。
用dfs,最后程序跑得时间是32ms,二进制枚举跑得时间是285ms,如此大的差距,原因就是二进制把所有组合都枚举完了,其中包含很多冗余状态,说白了就是大爆力。 DFS版:(32ms)
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#define MAXN 1111
using namespace std;
int cow[MAXN],vis[MAXN],N,D,K,ans;
void dfs(int idx,int cnt,int sum){
if(cnt == K){
int num = ;
for(int i = ;i < N;i ++)
if(cow[i] == (cow[i] & sum)) num++;
ans = max(ans,num);
return;
}
for(int i = idx;i < D;i ++){
if(!vis[i]){
vis[i] = ;
dfs(i+,cnt+,sum|( << i));
vis[i] = ;
}
}
}
int main(){
int tmp,kind;
// freopen("in.c","r",stdin);
while(~scanf("%d%d%d",&N,&D,&K)){
memset(cow,,sizeof(cow));
memset(vis,,sizeof(vis));
for(int i = ;i < N;i ++){
scanf("%d",&tmp);
for(int j = ;j < tmp;j ++){
scanf("%d",&kind);
cow[i] |= ( << (kind-));
}
}
ans = ;
dfs(,,);
printf("%d\n",ans);
}
return ;
}

二进制枚举版:(285ms)

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#define MAXN 1111
using namespace std;
int cow[MAXN];
int count_digit(int n){
int cnt = ;
for(int i = ;i < ;i ++)
if(n & ( << i)) cnt ++;
return cnt;
}
int main(){
int n,m,k,tmp,kind;
//freopen("in.c","r",stdin);
while(~scanf("%d%d%d",&n,&m,&k)){
memset(cow,,sizeof(cow));
for(int i = ;i < n;i ++){
scanf("%d",&tmp);
for(int j = ;j < tmp;j ++){
scanf("%d",&kind);
cow[i] |= ( << (kind-));
}
}
int ans = ;
for(int i = ;i < ( << m);i ++){
int sum = ,cnt = ;
if(count_digit(i) > k) continue;
for(int j = ;j < n;j ++)
if((cow[j] | i) == i) cnt++;
ans = max(ans,cnt);
}
printf("%d\n",ans);
}
return ;
}
 

最新文章

  1. JAVA读取TXT文件、新建TXT文件、写入TXT文件
  2. [BZOJ3991][SDOI2015]寻宝游戏
  3. SpringMVC @RequestBody问题:Unrecognized field , not marked as ignorable
  4. Samba服务器搭建配置
  5. DedeCMS Error:Tag disabled:&quot;php&quot;的解决办法
  6. Swift编程语言学习3.1排列
  7. jfinal框架教程-学习笔记
  8. Android dumpsys命令的使用
  9. Google 分布式关系型数据库 F1
  10. python web开发-flask中response,cookies,session对象使用详解
  11. mysql将表数据导出为txt或csv文件
  12. Java实践:一个简易的http server和client的java源码学习和总结。
  13. BSGS与扩展BSGS
  14. 深入MySQL复制(三):半同步复制
  15. Python之给程序传参数
  16. [zz]如何学习Polygon Mesh Processing这本书?
  17. modal 永久移除遮盖层
  18. MFC窗口阴影
  19. spark - Locality Level
  20. pta l3-7(天梯地图)

热门文章

  1. c#泛型方法重载
  2. mysql笔记之集群
  3. LA 3708 Graveyard(推理 参考系 中位数)
  4. object 属性 对象的继承 (原型, call,apply)
  5. 计算字符串和文件的MD5值
  6. Swift语言 1小时速学教程
  7. js 中对象的特性
  8. react native学习1-安装,执行
  9. VMWare Workstation 占用443端口导致apache启动不了
  10. Tasklist 命令的使用