BZOJ 1688: [Usaco2005 Open]Disease Manangement 疾病管理 状压DP + 二进制 + 骚操作
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.
题解:
树状数组中这是一个必备的工具,在状压 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; }