BZOJ1688 Disease Manangement 疾病管理(状压dp)

**

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

题意

有N头牛,一共有D种病,第i头牛患d_i种病,求数量最多的一个牛的集合,使这个集合中的牛的不同病数总和不超过k,求这个数量的大小。

思路

看病的最大数不超过15,嗯,这道题肯定用状压dp,对于第i头牛,我们可以用一个二进制数a[i]来表示其状态,若这头牛患第x种病,则a[i]的二进制数的第x位为1.a[i] |= (1<<(x-1))。
然后为了提高效率我们可以预处理一个数组cnt,对于状态i,cnt[i]等于i的二进制数中有多少个1。数组dp 可以表示对于某个病集合,符合这个病集合中有的病的牛有多少个。如:dp[5],5的二进制为101,则表示在n头牛中只有第一种病或只有第二种病或有第一第二种病的牛有多少。

代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 1001
#define inf 0x3f3f3f3f
int n, d, k;
int a[1001], dp[1<<15], cnt[1<<15];
int main(){
    int i, j, m, s;
    scanf("%d%d%d", &n,&d,&k);
    for(i=0; i < (1<<d); i++) cnt[i] = cnt[i>>1] + (i&1); //状态预处理
    for(i = 1; i <= n; i++){
        scanf("%d",&m);
        while(m--){
            scanf("%d",&s);
            a[i] |= (1<<(s-1));
        }
    }
    int ans = 0;
    for(i = (1<<d)-1; i >= 1; i--){
    	if(cnt[i]>k) continue;   //表示i这种状态的病集合,集合元素大过了k,跳过
        for(j = 1; j <= n; j++){
            s = a[j]&i;  
            if(s!=a[j]) continue;   //判断牛j有的病,i是否都有
            dp[i]++;  
            if(dp[i]>ans) ans = dp[i];
        }
    }
    printf("%d\n",ans);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值