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

POJ -- 2436的更多相关文章

  1. POJ 2436 二进制枚举+位运算

    题意:给出n头牛的得病的种类情况,一共有m种病,要求找出最多有K种病的牛的数目: 思路:二进制枚举(得病处为1,否则为0,比如得了2 1两种病,代号就是011(十进制就是3)),首先枚举出1的个数等于 ...

  2. POJ 2436 二进制枚举

    题意: 思路: 拆成二进制枚举 有哪个病毒在 判一判 就好了 //By SiriusRen #include <cstdio> #include <cstring> #incl ...

  3. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  4. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  5. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  6. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  7. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  8. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  9. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

随机推荐

  1. WPF ItemsControl 控件支持鼠标滚轮滑动

    此文章意在解决在WPF中ItemsControl类型的集合控件支持鼠标滚轮操作,并可控制滚动的速度. 第一步:给ItemsControl添加滚轮事件. this.listBox.AddHandler( ...

  2. 【Java咬文嚼字】关键字(二):八个基本数据类型

    本次谈谈java中的基本数据类型.java中8个基本数据类型说多不多说少也不少,相对简单的记忆方式:整数型(byte,short,int,long).浮点型(float,double).逻辑型(boo ...

  3. 暑假集训(1)第六弹 -----简单计算器(Hdoj1237)

    Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值.   Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算 ...

  4. Mysql备份数据库的一种方法

    今天添加了一个数据库自动备份的模块,mysql数据备份的方法有很多,可以对单个数据库备份,可以多个数据库备份,也可以对某一个表进行备份,可以只备份数据库的结构不备份数据,可以根据需要做不同处理,正好现 ...

  5. 如何通过CSS让DIV居中对齐

    给Div对应的CSS的添加如下设定即可: MARGIN-RIGHT: auto; MARGIN-LEFT: auto;

  6. C++输入输出缓冲区的刷新问题

    当我们对文件流进行操作的时候,它们与一个streambuf 类型的缓存(buffer)联系在一起.这个缓存(buffer)实际是一块内存空间,作为流(stream)和物理文件的媒介.例如,对于一个输出 ...

  7. Unix环境高级编程学习笔记——fcntl

    写这篇文正主要是为了介绍下fcntl,并将我自己在学习过程中的一些理解写下来,不一定那么官方,也有错误,希望指正,共同进步- fcntl: 一个修改一打开文件的性质的函数.基本的格式是 int fcn ...

  8. .net ajax式上传文件

    今天在这里介绍一下ajax上传文件.其实也不算是真的使用xmlhttprequest上传,只是使用了iframe实现了无刷新上传而已,最多也只算 是仿ajax上传文件.然而网上关于使用xmlhttpr ...

  9. 将选择的图片显示在listview中,并显示filename,path和type

    if (openFileDialog1.ShowDialog() == DialogResult.OK) { listView1.Items.Clear(); string[] files = ope ...

  10. MySQL的基本

    MySQL的基本语法 left JOIN 左表匹配右表 有没有内容全部匹配 SELECT Persons.LastName, Orders.OrderNo FROM Persons INNER JOI ...