POJ 2436 Disease Management
来源:互联网 发布:网络大电影监禁风暴 编辑:程序博客网 时间:2024/06/07 06:54
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.
* 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.
Output
* Line 1: M, the maximum number of cows which can be milked.
Sample Input
6 3 201 11 21 32 2 12 2 1
Sample Output
5
Hint
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).
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).
#include <ctime>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <string>#include <vector>#include <queue>#include <map>#include <set>#include <numeric>#include <algorithm>#include <functional>using namespace std;/* 解题思路:本题要求解的是选出最多的牛使得所有牛加起来的病菌总数<=K 每头牛的携带的病菌的状态可以用利用二进制压缩为一个整数,于是不难得出 dp[i][j]表示前i头牛且携带病菌状态为j时的最多的奶牛数 dp[i][j] = max{dp[i-1][j], dp[i-1][k]+1} 其中 k|arr[i] = j 另外由于空间限制采用滚动数组进行优化*/const int inf = 0x3f3f3f3f;const int maxn = (1<<15) + 10;int dp[2][maxn];int arr[1010];int count_bit(int x) { int ans = 0; while(x) { if(x&1) ans++; x >>= 1; } return ans;}int main() { //freopen("aa.in", "r", stdin); int N, D, K, d, x; scanf("%d %d %d", &N, &D, &K); memset(arr, 0, sizeof(arr)); for(int i = 1; i <= N; ++i) { scanf("%d", &d); for(int j = 0; j < d; ++j) { scanf("%d", &x); --x; arr[i] |= (1<<x); } } int id = 0; memset(dp, -inf, sizeof(dp)); dp[id][0] = 0; dp[id][arr[1]] = 1; for(int i = 1; i < N; ++i) { for(int j = 0; j < (1<<D); ++j) { if(dp[id][j] < 0) continue; dp[id^1][j] = max(dp[id^1][j], dp[id][j]); dp[id^1][j|arr[i+1]] = max(dp[id^1][j|arr[i+1]], dp[id][j] + 1); } id ^= 1; } int ans = 0; for(int i = 0; i < (1<<D); ++i) { if(count_bit(i) <= K) { ans = max(ans, dp[id][i]); } } printf("%d\n", ans); return 0;}
0 0
- POJ 2436 Disease Management
- POJ _2436--Disease Management
- PKU 2436 Disease Management
- pku 2436 Disease Management
- 【状态压缩&位运算】poj 2436 Disease Management
- Disease Management
- POJ-3268-Disease Management-(最短路径spfa)
- Disease
- 【POJ】1004 Financial Management
- POJ-1004:Financial Management
- POJ 1004 Financial Management
- POJ 1004 Financial Management
- [POJ]1004 Financial Management
- POJ 1004 Financial Management
- poj 1004 Financial Management
- poj 1004 Financial Management
- POJ - Financial Management
- POJ-1004-Financial Management
- 语言中函数传参的三种方式
- C++单例模式
- 第一周项目0 初识CSDN
- JSTL分页显示 动态页码
- CSS 小总结
- POJ 2436 Disease Management
- 第二周项目2-程序的多文件组织
- PM九步法
- TCP的流量控制和拥塞控制 (小结)
- Windows下编译Caffe并编译Matlab接口
- C语言栈实现逆波兰计算器
- 子串变位词
- Codeforces 482C Game with strings
- 咨询词汇