BZOJ1688|二进制枚举子集| 状态压缩DP
时间:2019-04-20
本文章向大家介绍BZOJ1688|二进制枚举子集| 状态压缩DP,主要包括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;
}
- 用最简单的图文带你看懂区块链
- 微信小游戏正式上线!小程序的一个类目,即点即玩!
- CentOS7安装GitLab、汉化、邮箱配置及使用
- Bootstrap源码分析之transition、affix
- Redis从单机到集群,一步步教你环境部署以及使用
- C++内存管理学习堆和栈
- 人人公司宣布收购美国卡车社区平台Trucker Path
- Mysql字符串截取总结:left()、right()、substring()、substring_index()
- c++程序的内存格局
- SpringMVC中利用@InitBinder来对页面数据进行解析绑定
- 门罗盘点:2017年那些AI击败人类的瞬间
- sizeof和strlen的区别
- mysql索引使用技巧及注意事项
- 条码支付新规,微信支付宝还好么?
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- linux下pip的安装步骤及使用详解
- CentOS7安装配置 Redis的方法步骤
- Linux下Oracle如何导入导出dmp文件详解
- Linux中samba服务器的搭建教程
- linux环境搭建图数据库neo4j的讲解
- Linux编程之ICMP洪水攻击
- linux搭建squid代理服务器的完整步骤
- 使用wget递归镜像网站
- 整理Linux中字符串的相关操作技巧
- Ubuntu 16.04下无法安装.deb的解决方法
- 关于linux中系统输入输出的管理详解
- Linux下IP设置脚本的实例及遇到问题解决办法
- Linux与Windows文件互传(VMWare)
- 如何测试Linux下tcp最大连接数限制详解
- Linux中利用Vim对文件进行密码保护的方法详解