BZOJ1688 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]
只是稍微接触过一点状压dp,没有一点思路。
首先预处理出 num[i] 表示状态i有几种病,用a[i]存每头牛的状态。
然后dp[i] 表示状态为i的最多牛数
那么转移方程为 dp[s] = max(dp[s] , dp[S] + 1); 其中s=a[i] | S;
#include<bits/stdc++.h>
using namespace std;
const int maxn=; const int M=<<;
int a[maxn],num[M],dp[M];
int main() {
for(int i=;i<M;i++) num[i]=num[i>>]+(i&);
int n,m,k;
while(~scanf("%d%d%d",&n,&m,&k)) {
memset(a,,sizeof(a));
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++) {
int x;
scanf("%d",&x);
while(x--) {
int val;
scanf("%d",&val);
a[i]|=<<(val-);//将第val位变为1
}
}
for(int i=;i<=n;i++) {
for(int S=(<<m)-;S>=;S--) {//枚举状态
int s=a[i]|S;//由S可以到达的状态s
if(num[s]>k) continue;
dp[s]=max(dp[s],dp[S]+);//那么s壮态数量 就可能更新
}
}
int ans=-;
for(int i=;i<M;i++) {
if(num[i]>k) continue;
ans=max(ans,dp[i]);
}
printf("%d\n",ans);
}
}
#include<bits/stdc++.h>using namespace std;const int maxn=1010;
const int M=1<<15;int a[maxn],num[M],dp[M];int main() { for(int i=0;i<M;i++) num[i]=num[i>>1]+(i&1); int n,m,k; while(~scanf("%d%d%d",&n,&m,&k)) { memset(a,0,sizeof(a)); memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) { int x; scanf("%d",&x); while(x--) { int val; scanf("%d",&val); a[i]|=1<<(val-1);//将第val位变为1 } } for(int i=1;i<=n;i++) { for(int S=(1<<m)-1;S>=0;S--) {//枚举状态 int s=a[i]|S;//由S可以到达的状态s if(num[s]>k) continue; dp[s]=max(dp[s],dp[S]+1);//那么s壮态数量 就可能更新 } } int ans=-1; for(int i=0;i<M;i++) { if(num[i]>k) continue; ans=max(ans,dp[i]); } printf("%d\n",ans); }}
BZOJ1688 Disease Manangement 疾病管理的更多相关文章
- 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP
[BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...
- 1688: [Usaco2005 Open]Disease Manangement 疾病管理( 枚举 )
我一开始写了个状压dp..然后没有滚动就MLE了... 其实这道题直接暴力就行了... 2^15枚举每个状态, 然后检查每头牛是否能被选中, 这样是O( 2^15*1000 ), 也是和dp一样的时间 ...
- 1688: [Usaco2005 Open]Disease Manangement 疾病管理
1688: [Usaco2005 Open]Disease Manangement 疾病管理 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 413 So ...
- 【状压dp】【bitset】bzoj1688 [Usaco2005 Open]Disease Manangement 疾病管理
vs(i)表示患i这种疾病的牛的集合. f(S)表示S集合的病被多少头牛患了. 枚举不在S中的疾病i,把除了i和S之外的所有病的牛集合记作St. f(S|i)=max{f(S)+((St|vs(i)) ...
- bzoj1688: [Usaco2005 Open]Disease Manangement 疾病管理
思路:状压dp,枚举疾病的集合,然后判断一下可行性即可. #include<bits/stdc++.h> using namespace std; #define maxs 400000 ...
- [Usaco2005 Open]Disease Manangement 疾病管理 BZOJ1688
分析: 这个题的状压DP还是比较裸的,考虑将疾病状压,得到DP方程:F[S]为疾病状态为S时的最多奶牛数量,F[S]=max{f[s]+1}; 记得预处理出每个状态下疾病数是多少... 附上代码: # ...
- 【bzoj1688】[USACO2005 Open]Disease Manangement 疾病管理
题目描述 Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) is running through the farm. Far ...
- 【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 ...
- BZOJ 1688: [Usaco2005 Open]Disease Manangement 疾病管理
Description Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) is running through the fa ...
随机推荐
- Oracle和Mysql的区别 转载
一.并发性 并发性是oltp数据库最重要的特性,但并发涉及到资源的获取.共享与锁定. mysql:mysql以表级锁为主,对资源锁定的粒度很大,如果一个session对一个表加锁时间过长,会让其他se ...
- 利用django中间件CsrfViewMiddleware防止csrf攻击
一.在django后台处理 1.将django的setting中的加入django.contrib.messages.middleware.MessageMiddleware,一般新建的django项 ...
- 阿里云WindowsServer2012安装IIS失败
本文地址:http://www.cnblogs.com/drfxiaoliuzi/p/6388417.html 首先,向微软官方论坛的大神致敬: https://social.technet.micr ...
- javascript总结24:Array常用的队列操作和排序方法
1 数组-引用类型 JavaScript中的内置对象 复习数组的使用 两种创建数组的方式 Array对象的属性 length 获取数组的长度(元素个数) 2 常用方法 : 检测数组 instanceo ...
- RF和GBDT的区别
Random Forest 采用bagging思想,即利用bootstrap抽样,得到若干个数据集,每个数据集都训练一颗树. 构建决策树时,每次分类节点时,并不是考虑全部特征,而是从特征候选集中选取 ...
- 浅谈Oracle12c 数据库、用户、CDB与PDB之间的关系
名词介绍: 数据库:数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生 于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以 后,数据管理不再仅仅是存储和管理 ...
- Discovering Gold LightOJ - 1030 (概率dp)
You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave c ...
- 对于nginx为什么能提高性能
对于后端是动态服务来说,比如Java和PHP.这类服务器(如JBoss和PHP-FPM)的IO处理能力往往不高.Nginx有个好处是它会把Request在读取完整之前buffer住,这样交给后端的就是 ...
- Reporting Service服务SharePoint集成模式安装配置(5、安装 SQL SERVER 2012 SP1产品)
有过SQL2012 数据库安装经验的,可以跳过这一步骤直接进入第五步骤:RS外接程序的安装 数据库安装工具:SQLServer2012 SP1 Name:SQLServer2012SP1-FullS ...
- ifstat命令行统计网络流量
下载 ifstat , http://gael.roualland.free.fr/ifstat/ifstat-1.1.tar.gz tar xzvf ifstat-1.1.tar.gz ...