POJ 2436 Disease Management

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 2
0
1 1
1 2
1 3
2 2 1
2 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值