【状态压缩&位运算】poj 2436 Disease Management
来源:互联网 发布:win7进入windows后黑屏 编辑:程序博客网 时间:2024/05/21 22:39
poj 2436 Disease Management
http://poj.org/problem?id=2436
问题描述:n个奶牛携带至多d种病毒,最多选出多少头奶牛使得携带病毒的种类不超过k种?
思路: 状态压缩+位运算
位运算真的很神奇,很容易想到将每一头奶牛的患病情况抽象成一串二进制序列,0011表示携带第一二种病毒,压缩成十进制用位运算的|来判断,怎么判断呢,如果枚举所有牛的组合会有2^N ->2 ^ 100种情况,每种情况按位或|再判断复杂度太高?!!!
可以暴力结果,最终的结果无非是0000~1111(如果d = 4的话,注意是d不是k),其中1的个数超过k的排除掉,假设状态为x,与每一头牛的情况|,若x==x|row[i]说明第i头牛最终落到这种x状态,累加起来取最大值即可,复杂度O(n×2^d)
参考代码
#include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<vector>#include<set>#include<map>#include<algorithm>#include<sstream>#define eps 1e-9#define pi acos(-1)#define long long ll#define M 10#define N 1010using namespace std;const int _max = 170 + 10;int x,m,n,d,k,row[N];bool judge(int x){ int ans = 0; while(x){ if(x&1) ans++; x>>=1; } return (ans<=k);}int main(){ #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); #endif // ONLINE_JUDGE while(~scanf("%d%d%d",&n,&d,&k)){ for(int i = 1; i <= n; ++ i){ row[i] = 0; scanf("%d",&m); for(int j = 0; j < m; ++ j){ scanf("%d",&x); row[i]^=1<<(x-1);//状态压缩 } } int ans,tar = -1; for(int i = 0; i < (1<<d); ++ i){ if(!judge(i)) continue; ans = 0; for(int j = 1; j <= n; ++ j) if((i|row[j]) == i) ans++; if(tar < ans) tar = ans; } printf("%d\n",tar); } return 0;}
- 加粗
Ctrl + B
- 斜体
Ctrl + I
- 引用
Ctrl + Q
- 插入链接
Ctrl + L
- 插入代码
Ctrl + K
- 插入图片
Ctrl + G
- 提升标题
Ctrl + H
- 有序列表
Ctrl + O
- 无序列表
Ctrl + U
- 横线
Ctrl + R
- 撤销
Ctrl + Z
- 重做
Ctrl + Y
1 0
- 【状态压缩&位运算】poj 2436 Disease Management
- POJ 2436 Disease Management
- POJ _2436--Disease Management
- PKU 2436 Disease Management
- pku 2436 Disease Management
- 位运算状态压缩
- poj 炮兵阵地 状态压缩DP + 位运算
- poj 2777 Count Color(线段树、状态压缩、位运算)
- POJ 3628 Bookshelf 2(状态压缩DP+位运算)
- POJ 1753 Flip Game(bfs+状态压缩+位运算)
- hdu1429位运算状态压缩
- 位运算(状态压缩)
- Disease Management
- zoj 2297【DP+位运算状态压缩】
- 位运算简介及实用技巧--状态压缩
- 位运算状态压缩——跳棋
- POJ-3268-Disease Management-(最短路径spfa)
- Poj-3570 Fund Management(状态压缩动态规划)
- 并查集+二分 hnu13433 Dragons
- 第三周--【项目 - 求集合并集】
- 第4周项目1 建立单链表
- 左旋右旋 如何写好不犹豫写好代码
- iOS9的新特性
- 【状态压缩&位运算】poj 2436 Disease Management
- 第四周:建立单链表
- poj 3421
- LeetCode 069 Sqrt(x)
- [Python进阶-2]模块和包的复习,以及第三方模块的安装
- 第四周项目三 单链表应用(1)
- 单链表的应用
- 项目2 -- 建立链栈算法库
- 第四周项目4-建设双链表算法库