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;elsereturn 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;
}

转载于:https://www.cnblogs.com/fisherss/p/10742801.html

BZOJ1688|二进制枚举子集| 状态压缩DP相关推荐

  1. 第 256 场力扣周赛(状态压缩+dp,二进制子序列的动规、940)

    第 256 场力扣周赛 有事没做,来看一下题 5854. 学生分数的最小差值 题目描述 给你一个 下标从 0 开始 的整数数组 nums ,其中 nums[i] 表示第 i 名学生的分数.另给你一个整 ...

  2. LeetCode 2044. 统计按位或能得到最大值的子集数目(状态压缩DP)

    文章目录 1. 题目 2. 解题 1. 题目 给你一个整数数组 nums ,请你找出 nums 子集 按位或 可能得到的 最大值 ,并返回按位或能得到最大值的 不同非空子集的数目 . 如果数组 a 可 ...

  3. 状态压缩DP AcWing算法提高课 (详解)

    基础课的状态压缩点这里 基础课中 蒙德里安的梦想 属于 棋盘式状态压缩dp,最短Hamilton路径 属于 集合状态压缩dp 1064. 小国王(棋盘式/基于连通性) 这种棋盘放置类问题,在没有事先知 ...

  4. LeetCode 1879. 两个数组最小的异或值之和(状态压缩DP)

    文章目录 1. 题目 2. 解题 2.1 回溯 2.2 状态压缩DP 1. 题目 给你两个整数数组 nums1 和 nums2 ,它们长度都为 n . 两个数组的 异或值之和 为 (nums1[0] ...

  5. LeetCode 1723. 完成所有工作的最短时间(DFS+剪枝 / 状态压缩DP)

    文章目录 1. 题目 2. 解题 2.1 DFS 2.2 状态压缩DP 265 / 3871, 前6.85% 前3题题解: LeetCode 5649. 解码异或后的数组(位运算) LeetCode ...

  6. 0x56. 动态规划 - 状态压缩DP(习题详解 × 7)

    目录 Problem A. 最短Hamilton路径 ProblemB. 蒙德里安的梦想 Problem C. Corn Fields Problem D. 小国王 Problem E. 炮兵阵地 P ...

  7. 状态压缩dp入门 第一题 POJ 3254 Corn Fields

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6460   Accepted: 3436 Descr ...

  8. 状态压缩DP(大佬写的很好,转来看)

    奉上大佬博客 https://blog.csdn.net/accry/article/details/6607703 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的 ...

  9. [NOIP2016]愤怒的小鸟 状态压缩dp

    题目描述 Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可以用它向第一象限发射一只红色的小鸟,小鸟们的飞行轨迹均为形 ...

最新文章

  1. Python 类的定义、继承及使用对象
  2. 汽车车牌识别系统实现(四)--字符识别+代码实现
  3. docker-Consul的概述及consul集群环境的搭建
  4. windows10和ubuntu双系统win10时间不正确
  5. 如何用gitbook写文档并存到github上
  6. 02 Oracle 批量导出建表语句和数据
  7. 树莓派4B安装windows xp windows 95( windows xp windows 95 for raspberry pi 4B)
  8. EmEditor Professional v14/15/16/17/18 最新版 注册码 2000组(终身授权)
  9. 利用Python实现简单的相似图片搜索
  10. LR9.10破解方法。
  11. Vulkan教程 - 08 着色器及编译SPIR-V
  12. Android 阻止AlertDialog dismiss
  13. Linux管道通信多次读写,linux进程通信之(二):管道的读与写
  14. python爬虫模式_python爬虫的入门试炼
  15. 手把手教你 合并分支到master上
  16. 下载到的电子书格式是Mobi,这种格式能否在WINDOWS电脑上打开?
  17. 手机报彩信易操作平台
  18. Fedora linux root登录和ssh连接
  19. P7毕业项目,猫狗大战。详解,含全部代码
  20. 下载谷歌play应用_选择在现有应用中使用Google Play应用签名

热门文章

  1. python 两阶段聚类_使用Python进行层次聚类
  2. apache评分表的意义_APACHE评分系统及评分表
  3. mysql子查询设置_什么是mysql子查询?如何利用子查询进行过滤?
  4. react native 组件之switch组件的用法
  5. 再见SpringMVC!小程序开发工程师岗位职责
  6. 【网页前端设计Front end】JavaScript教程.下(看不懂你来打我)
  7. 【深度学习入门到精通系列】阿里云人工智能平台的使用方法
  8. python【力扣LeetCode算法题库】892-三维形体的表面积
  9. BroadCastReceiver简介
  10. java微信oppo,OPPO实现全球首次5G微信视频通话,国产手机满分操作