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的更多相关文章

  1. UVA 11825 - Hackers&#39; Crackdown 状态压缩 dp 枚举子集

    UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集 ACM 题目地址:option=com_onlinejudge&Itemid=8&page=sh ...

  2. P5911 [POI2004]PRZ (状态压缩dp+枚举子集)

    题目背景 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 题目描述 桥已经很旧了, 所以它不能承受太重的东西.任何时候队伍在桥上的人都不能超过一定的限制. 所以这只队伍过桥时 ...

  3. BFS+状态压缩DP+二分枚举+TSP

    http://acm.hdu.edu.cn/showproblem.php?pid=3681 Prison Break Time Limit: 5000/2000 MS (Java/Others)   ...

  4. 【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 ...

  5. 状态压缩dp相关

    状态压缩dp 状态压缩是设计dp状态的一种方式. 当普通的dp状态维数很多(或者说维数与输入数据有关),但每一维总 量很少是,可以将多维状态压缩为一维来记录. 这种题目最明显的特征就是: 都存在某一给 ...

  6. [动态规划]状态压缩DP小结

     1.小技巧 枚举集合S的子集:for(int i = S; i > 0; i=(i-1)&S) 枚举包含S的集合:for(int i = S; i < (1<<n); ...

  7. POJ 3311 Hie with the Pie(Floyd+状态压缩DP)

    题是看了这位的博客之后理解的,只不过我是又加了点简单的注释. 链接:http://blog.csdn.net/chinaczy/article/details/5890768 我还加了一些注释代码,对 ...

  8. POJ3254 - Corn Fields(状态压缩DP)

    题目大意 给定一个N*M大小的土地,土地有肥沃和贫瘠之分(每个单位土地用0,1来表示贫瘠和肥沃),要求你在肥沃的单位土地上种玉米,如果在某个单位土地上种了玉米,那么与它相邻的四个单位土地是不允许种玉米 ...

  9. POJ 1185 (状态压缩DP)

    中文题目,题意就不说了. 不得不说这是一道十分经典的状态压缩DP的题目. 思路: 通过分析可以发现,第i行的格子能不能放大炮仅与第i-1和i-2行的放法有关,而与前面的放法无关,因此,如果我们知道了i ...

随机推荐

  1. build.xml编译报错Specified VM install not found: type Standard VM, name jdk1.7.0_45

    build.xml编译打包时报错: 解决方法: build.xml  ——  右键 ——  Run As —— External Tools Configuration 在这个页面的顶端就会看到有红叉 ...

  2. Mysql5.8解压版安装问题:TCP/IP, --shared-memory, or --named-pipe should be configured on NT OS

    问题描述: cmd显示如下: .err文件显示: [ERROR] [MY-010131] [Server] TCP/IP, --shared-memory, or --named-pipe shoul ...

  3. linux基础命令--groupmod 修改组定义

    描述 groupmod命令用于修改系统上的组定义. groupmod命令通过更改组数据库(the group database)里的相关条目来修改指定的组. 语法 groupmod [选项] GROU ...

  4. NoSQL入门

    NoSQL(Not Only SQL)入门: *没有Fixed Schema *没有关系型数据储存在系统中 * 在大数据方面NoSQL有更好的表现 * 支持unstructured data - 不同 ...

  5. 将分支推送到远程存储库时遇到错误: Git failed with a fatal error. TaskCanceledException encountered.

    解决:https://blog.csdn.net/dw33xn/article/details/79951714 修改下配置文件即可

  6. css实现礼券效果

    <template> <div class="demo"> <div class="stamp stamp01"> < ...

  7. Android使用https与服务器交互的正确姿势

    HTTPS 使用 SSL 在客户端和服务器之间进行加密通信,错误地使用 SSL ,将会导致其它人能够拦截网络上的应用数据. 使用一个包含公钥及与其匹配的私钥的证书配置服务器,作为 SSL 客户端与服务 ...

  8. 《linux就该这么学》第十七节课:第18,19,23章,mariadb数据库、PXE无人值守安装系统和openldap目录服务。

    第23章 (借鉴请改动) openldap数据的特点:1.短小.2.读取次数较多 上述说明: openLDAP服务端配置:     1.yum install -y openldap openldap ...

  9. Windows 10,鼠标右键-发送到-桌面快捷方式缺失解决方法

    1-双击“我的电脑”. 进到这里 2-路径框修改为“shell:Sendto”,回车. 3-把“桌面快捷方式”黏贴到Sendto文件夹下

  10. 线段树 HDU-1754 I Hate It

    附上原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某 ...