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.

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).         

#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
原创粉丝点击