BZOJ1688|二进制枚举子集| 状态压缩DP
Disease Manangement 疾病管理
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. 有N头牛,它们可能患有D种病,现在从这些牛中选出若干头来,但选出来的牛患病的集合中不过超过K种病.
Output
- Line 1: M, the maximum number of cows which can be milked.
Sample Input 1
6 3 2
0———第一头牛患0种病
1 1——第二头牛患一种病,为第一种病.
1 2
1 3
2 2 1
2 2 1
Sample Output 1
5
OUTPUT DETAILS:
If FJ milks cows 1, 2, 3, 5, and 6, then the milk will have only two
diseases (#1 and #2), which is no greater than K (2).
Source
[BZOJ1688][Usaco2005 Open]
网上找的代码,已经加上详细注解,明天补题写一遍
二进制枚举的做法:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,d,k;
int N[1000+5];
//统计有多少个1
bool judge(int x)
{
int c=0;
while(x)
{
c++; // 将x转化为2进制,看含有的1的个数。
x&=(x-1); //将最低的为1的位变成0
}
if(c<=k) //限制个数
return 1;
else
return 0;
}
int main()
{
int s,t,total=0;
scanf("%d%d%d",&n,&d,&k);
for(int i=0; i<n; i++)
{
cin>>s;
for(int j=0; j<s; j++)
{
cin>>t;
N[i]|=1<<(t-1); //1<<t-1(1的二进制数整体向左移t-1位)
//一起把二进制数的位数对应着来看,这两个数在这一位上有1的结果就是1,否则是0
}
}
for(int i=0; i<(1<<d); i++) //i<(1<<d)是当i不小于(1左移d位的数)时终止循环,枚举各子集对应的编码0,1,2,..2^d-1
{
if(judge(i)) //判断 患病子集为i 是否满足<d种
{
int f = 0;
for(int j=0; j<n; j++)
{
//枚举n头牛 如果当前牛患病的个数小于 当前的i(患病子集),说明满足条件 ; 不满足就不要这头牛
if((N[j]|i)==i) f++; //对应N[j]与i的并集与i相等,说明N[j]是它的子集
}
if(f>total) //更新最大值
total=f;
}
}
cout<<total<<endl;
return 0;
}
状态压缩DP的做法:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[1005],n,d,k,ans=0,ma=0,f[1<<16];
bool check(int x)
{
int cnt=0;
//枚举d位 (患病状态:1为患病 0为不换病)
for (int i=1;i<=d;i++)
{
if (((1<<i-1)&x)>0) cnt++; //判断1~d各个位是否等于1,等于1 cnt就++ (表示患病数+1)
if (cnt>k) return false; //比k种病多 就返回 false(表示该方案不可行)
}
return true;
}
int main()
{
scanf("%d%d%d",&n,&d,&k);
for (int i=1,x,y;i<=n;i++)
{
scanf("%d",&x);
for (int j=1;j<=x;j++)
{
scanf("%d",&y);
a[i]=a[i]|(1<<(y-1)); //存储状态:患病方案(10010 表示第2/5患病 1/3/4不换病)
}
}
for (int i=1;i<=n;i++) //枚举n头牛 a[i]已表示这头牛的患病状态
for (int j=(1<<d);j>=1;j--) //枚举1<<d种患病状态
f[a[i]|j]=max(f[a[i]|j],f[j]+1); //a[i] | j 理解为:n头牛累加的患病总和(d种患病 和 n头牛患病状态 作或操作 就是求集合的并)
//筛选患病总数小于k的方案 1<<d个(从n头牛中选择i个) 判断每个(选牛)集合中满足条件(患病总数小于k)的集合
for (int i=1;i<=(1<<d);i++)
if (check(i))
ans=max(ans,f[i]);
printf("%d",ans);
return 0;
}
BZOJ1688|二进制枚举子集| 状态压缩DP的更多相关文章
- UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集
UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集 ACM 题目地址:option=com_onlinejudge&Itemid=8&page=sh ...
- P5911 [POI2004]PRZ (状态压缩dp+枚举子集)
题目背景 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 题目描述 桥已经很旧了, 所以它不能承受太重的东西.任何时候队伍在桥上的人都不能超过一定的限制. 所以这只队伍过桥时 ...
- BFS+状态压缩DP+二分枚举+TSP
http://acm.hdu.edu.cn/showproblem.php?pid=3681 Prison Break Time Limit: 5000/2000 MS (Java/Others) ...
- 【bzoj1688】[USACO2005 Open]Disease Manangement 疾病管理 状态压缩dp+背包dp
题目描述 Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) is running through the farm. Far ...
- 状态压缩dp相关
状态压缩dp 状态压缩是设计dp状态的一种方式. 当普通的dp状态维数很多(或者说维数与输入数据有关),但每一维总 量很少是,可以将多维状态压缩为一维来记录. 这种题目最明显的特征就是: 都存在某一给 ...
- [动态规划]状态压缩DP小结
1.小技巧 枚举集合S的子集:for(int i = S; i > 0; i=(i-1)&S) 枚举包含S的集合:for(int i = S; i < (1<<n); ...
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
题是看了这位的博客之后理解的,只不过我是又加了点简单的注释. 链接:http://blog.csdn.net/chinaczy/article/details/5890768 我还加了一些注释代码,对 ...
- POJ3254 - Corn Fields(状态压缩DP)
题目大意 给定一个N*M大小的土地,土地有肥沃和贫瘠之分(每个单位土地用0,1来表示贫瘠和肥沃),要求你在肥沃的单位土地上种玉米,如果在某个单位土地上种了玉米,那么与它相邻的四个单位土地是不允许种玉米 ...
- POJ 1185 (状态压缩DP)
中文题目,题意就不说了. 不得不说这是一道十分经典的状态压缩DP的题目. 思路: 通过分析可以发现,第i行的格子能不能放大炮仅与第i-1和i-2行的放法有关,而与前面的放法无关,因此,如果我们知道了i ...
随机推荐
- linux基础命令--groupdel 删除群组
描述 groupdel命令用于删除用户组. groupdel命令会去修改系统下的/etc/group和/group/gshadow文件,删除有关用户组的所有项目(一般来说使用groupadd或user ...
- robot framework 提示‘pybot 不是内部命令’
command: pybot.bat --argumentfile c:\*\RIDEzsxpul.d\argfile.txt --listener C:\Python27\lib\site-pack ...
- Oracle数据泵远程导入数据
查看现存镜像目录 select * from dba_directories; 创建镜像目录 create or replace directory my_dir as 'local_dir' ; 把 ...
- pc端字体大小自适应几种方法
$(window).resize(function ()// 绑定到窗口的这个事件中 { var whdef = 100/1920;// 表示1920的设计图,使用100PX的默认值 var wH ...
- MPLS的模拟学习过程
1.场景拓扑 使用小凡模拟器搭建了如下网络拓扑,使用的镜像为:c3640-jk9o3s-mz.122-15.T9.bin 相关的配置在下方 如果重复实验,需要清空设备的配置,知道路由器的密码,操作步骤 ...
- java实现控件的移动及使用鼠标改变控件大小
package cn.com.test; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; ...
- Java集合框架面试题目
1.为什么Map接口不继承Collection 接口? Set是无序集合,并且不允许重复的元素 List是有序的集合,并且允许重复的元素 而Map是键值对 它被视为是键的set和值的set的组合 Ma ...
- linux----------fedora 27 如何打开ssh,可以远程链接
1.vim /etc/ssh/ssh_config 打开ssh的配置文件 2.打开22端口 3.重启sshd:systemctl restart sshd 4.设置sshd开机启动:systemc ...
- ssl证书 以及phpstudy配置ssl证书
首先,确保你的Apache编译了SSL模块,这是支持SSL证书必要的条件(如果没有,请编译,[打开phpstudy]>[其他选项菜单]>[PHP扩展]>[php-openssl]前面 ...
- React-typescript-antd 常见问题
一.The key 'Accept' is not sorted alphabetically //tslint.json { "extends": ["tslint:r ...