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. vue加载流程

    首先加载main.js,main.js中new一个vue实例,这个实例中会有一个id="app"映射到app.vue,启动时候首页映射到index.html,其中<div i ...

  2. SCRIPT438: 对象不支持“indexOf”属性或方法

    SCRIPT438: 对象不支持“indexOf”属性或方法 indexOf()的用法:返回字符中indexof(string)中字串string在父串中首次出现的位置,从0开始!没有返回-1:方便判 ...

  3. wrapper induction随笔

    本文是一篇介绍Wrapper Induction的阅读笔记,原文详见<Wrapper induction:Efficiency and expressiveness>. Wrapper I ...

  4. 马拉车算法,mannacher查找最长回文子串

    作用: 在线性时间内找到一个字符串的最大回文子串 原理: 奇偶变换:为处理字符串方便,现将给定的任意字符串进行处理,使所有可能的奇数/偶数长度的回文子串都转换成了奇数长度. 具体就是在每个字符的两边都 ...

  5. pc端字体大小计算以及echart中字体大小计算

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  6. PyCharm:no module named * 解决方法

    1.成功安装模块,无法导入 今天安装完模块pyppeteer,pycharm导入失败,从python的Lib下可以清楚的看到已经安装成功 2.添加当前python环境,不使用默认项目的环境 file& ...

  7. EF(一)DB First

    “Database First”模式我们称之为“数据库优先”,前提是你的应用已经有相应的数据库,你可以使用EF设计工具根据数据库生成数据数据类,你可以使用Visual Studio模型设计器修改这些模 ...

  8. C# 读取xml——XmlReader和XElement

    1.有些xml文件头部有DTD,程序解析的时候会报错 如:其他信息: 打开外部 DTD“file:///E:/PM数据/MeContext=CDF2775/MeasDataCollection.dtd ...

  9. android 开发设计模式---Strategy模式

    假设我们要出去旅游,而去旅游出行的方式有很多,有步行,有坐火车,有坐飞机等等.而如果不使用任何模式,我们的代码可能就是这样子的. 12345678910111213141516171819202122 ...

  10. Java EE开发技术课程第三周

    一.分析Filter例子: @WebFilter(filterName="log",urlPatterns={"/*"})//创建一个LOgFilter类pub ...