POJ -- 2436
Description
Input
* 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
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的更多相关文章
- POJ 2436 二进制枚举+位运算
题意:给出n头牛的得病的种类情况,一共有m种病,要求找出最多有K种病的牛的数目: 思路:二进制枚举(得病处为1,否则为0,比如得了2 1两种病,代号就是011(十进制就是3)),首先枚举出1的个数等于 ...
- POJ 2436 二进制枚举
题意: 思路: 拆成二进制枚举 有哪个病毒在 判一判 就好了 //By SiriusRen #include <cstdio> #include <cstring> #incl ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
随机推荐
- Ibatis学习记录
几大要素:1.jdbc.properties //数据库连接配置2.SqlMapContext.xml //主配置文件3.user_SqlMap.xml //映射文件4.三层框架 创建Ibatis工程 ...
- jQuery动态效果实例
jQuery常见的动态效果: 隐藏/显示效果: 1.(1):隐藏,显示:通过 jQuery,使用 hide() 和 show() 方法可以用来隐藏和显示 HTML 元素. (2):隐藏/显示的速度: ...
- linux命令之解压与压缩
解压 tar –xvf file.tar //解压 tar包 tar -xzvf file.tar.gz //解压tar.gz tar -xjvf file.tar.bz2 //解压 tar.bz2 ...
- python-MySQLdb-练习
看完视频,自己练习一遍. 还是遇到问题,不过最终还是解决了.贴上完成的代码. CREATE TABLE `NewTable` ( `acctid` int(11) NOT NULL AUTO_INCR ...
- What is the Xcopy Command?:
Quote from: http://pcsupport.about.com/od/commandlinereference/p/xcopy-command.htm The xcopy command ...
- Java面向对象程序设计--与C++对比说明:系列3(Java 继承机制)
继承(inheritance)背后的核心思想是: bonus = b; } } Java没有像C++那样提供多继承机制,但提供了接口机制,在后面我们将详细探究接口机制的实现 ...
- 【CODECHEF】【phollard rho + miller_rabin】The First Cube
All submissions for this problem are available. Read problems statements in Mandarin Chinese and Rus ...
- 360极速浏览器在XP系统下的一个bug
今天在做页面开发的时候,发现360浏览器在XP系统下不支持focus事件,而主流的IE(包括IE6),firefox,chrome都没有这个问题.前段开发的悲剧啊,各种浏览器兼容性的问题.
- erlang 里的if 和 case
case Expression of Pattern1 [when Guard1] -> Expr_seq1; Pattern2 [when Guard2] -> Expr_seq2; … ...
- 递归查找某个目录下是否存在NOTICE文件
从Catalogs.txt文件中,读取待检查的目录列表.检查这些目录中,是否存在NOTICE文件,如果没有则检查它的父目录,直到cd ..到Repository目录. 如果cd ..到Reposito ...