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.

题解:

$lowbit(i)$ 的定义为 $i$ 的最后一个 $1$ 所对应的那个二进制串(我好像也不太能用一句话解释得清).
树状数组中这是一个必备的工具,在状压 DP 中用这个也是十分方便的. 
例如,要更新 $i$ 的二进制形式中 $1$ 的数量就可以用 $Num(i-lowbit(i))+1$ 来更新.
在本题中,我们先预处理出每一个患病数小于等于 $k$ 的所有子集.
每次读入的时候枚举一下,并更新到答案即可. 
#include <bits/stdc++.h>
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 100000
using namespace std;
int F[1 << 16], s[1 << 16], v[1 << 16], f[1 << 16];
int n, D, k, tot = 0, ans = 0;
int main()
{
// setIO("input");
scanf("%d%d%d",&n,&D,&k);
for(int i = 1; i < (1 << D); ++i)
{
s[i] = s[i - (i & -i)] + 1;
if(s[i] <= k) v[++tot] = i;
}
for(int i = 1; i <= n; ++i)
{
int a, b;
scanf("%d",&a);
int t = 0;
for(int j = 1; j <= a; ++j)
{
scanf("%d",&b);
t += (1 << (b - 1));
}
for(int j = 1; j <= tot; ++j)
{
if((v[j] & t) == t)
++f[j], ans = max(ans, f[j]);
}
}
printf("%d\n",ans);
return 0;
}

  

BZOJ 1688: [Usaco2005 Open]Disease Manangement 疾病管理 状压DP + 二进制 + 骚操作的更多相关文章

  1. 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP

    [BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...

  2. BZOJ 1688: [Usaco2005 Open]Disease Manangement 疾病管理

    Description Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) is running through the fa ...

  3. 1688: [Usaco2005 Open]Disease Manangement 疾病管理( 枚举 )

    我一开始写了个状压dp..然后没有滚动就MLE了... 其实这道题直接暴力就行了... 2^15枚举每个状态, 然后检查每头牛是否能被选中, 这样是O( 2^15*1000 ), 也是和dp一样的时间 ...

  4. 1688: [Usaco2005 Open]Disease Manangement 疾病管理

    1688: [Usaco2005 Open]Disease Manangement 疾病管理 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 413  So ...

  5. 【BZOJ】1688: [Usaco2005 Open]Disease Manangement 疾病管理(状压dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1688 很水的状压.. 提交了很多次优化的,但是还是100msT_T #include <cst ...

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

  7. bzoj1688: [Usaco2005 Open]Disease Manangement 疾病管理

    思路:状压dp,枚举疾病的集合,然后判断一下可行性即可. #include<bits/stdc++.h> using namespace std; #define maxs 400000 ...

  8. [Usaco2005 Open]Disease Manangement 疾病管理 BZOJ1688

    分析: 这个题的状压DP还是比较裸的,考虑将疾病状压,得到DP方程:F[S]为疾病状态为S时的最多奶牛数量,F[S]=max{f[s]+1}; 记得预处理出每个状态下疾病数是多少... 附上代码: # ...

  9. 【状压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)) ...

随机推荐

  1. Train Problem II HDU 1023 卡特兰数

    Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want ...

  2. AngularJS:一行JS代码实现控件验证效果

    如上图所示,我们需要实现如下这些验证功能: 控件都是必输控件 都需要控制最大长度 第一次打开页面,控件不能显示为错误状态 输入内容再清空后,必输控件需要显示为错误状态 只有所有输入合法后,发布按钮才能 ...

  3. 动态加入的HTML的自己主动渲染

    这两天在写一个用EasyUI的前台,遇到动态向Layout加入HTML内容时没有自己主动渲染的问题.查了一下网上的资料后得以解决.详细例如以下: $("#content").htm ...

  4. 集成CCFlow工作流与GPM的办公系统驰骋CCOA介绍(二)

    GPM怎样控制菜单权限以及菜单的增删显示 因为CCOA中仅仅有属于admin才干够进行权限管理与流程设计.password为pub. 1.加入CCOA功能菜单 进入GPM后,找到编号为CCOA的信息后 ...

  5. ubuntu下7z文件的解压方法

    apt-get install p7zip-full 控制台会打出以下信息: 正在读取软件包列表... 完成正在分析软件包的依赖关系树       正在读取状态信息... 完成       建议安装的 ...

  6. 更改printk打印级别【转】

    本文转载自:http://blog.csdn.net/weed_hz/article/details/8949140 1.查看当前控制台的打印级别 cat /proc/sys/kernel/print ...

  7. constraint和index--转载

    primary key和unique约束是要依赖index的,下面通过试验来看看他们之间的依赖关系!       SQL> select * from tt;   ID NA --------- ...

  8. bzoj 1034 [ ZJOI 2008 ] 泡泡堂BNB —— 贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1034 一开始想了个很麻烦的贪心做法,对于每个 a[i],找第一个大于它的 b 匹配…… 然后 ...

  9. 85.Ext.Window

    转自:https://chenjumin.iteye.com/blog/668421 1.主要配置项:       closable:是否允许关闭窗口,默认为true.       closeActi ...

  10. python-day3 元组(tuple),列表(list),字典(dict)

    1.元组 tuple 有序数据,元组数据不可更改,若元组中有列表,可更改元组中的列表值里的值 元组中以","分开,若只有一个值就不是元组 包含各种数据类型 索引取值:t(2,0.0 ...