雕刻时光

just do it……nothing impossible
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

位运算——pku2436患病的奶牛

Posted on 2011-09-05 21:09  huhuuu  阅读(200)  评论(0编辑  收藏  举报

患病的奶牛N只,D种病毒,最多包含K种病毒,求包含最多有几头牛

6 3 2
0
1 1
1 2
1 3
2 2 1
2 2 1
看到D的范围比较小<=15,用011,表示患了第2,3钟病毒的奶牛a[i]
在枚举K种病毒,如110  (用STL里的排列函数枚举)m
如m|a[i]==m,说明a[i]包括在m里,add++
记录最大值
View Code
#include<algorithm>
#include
<iostream>
#include
<string.h>
#include
<stdio.h>
using namespace std;

int a[1009];
bool b[19];

int main()
{
int n,d,k;
while(scanf("%d%d%d",&n,&d,&k)!=EOF)
{
int i,t,j,temp;
for(i=0;i<n;i++)
{
scanf(
"%d",&t);

a[i]
=0;
for(j=0;j<t;j++)
{
scanf(
"%d",&temp);
a[i]
+=1<<(temp-1);
}
}
memset(b,
0,sizeof(b));

for(i=0;i<k;i++)
{
b[d
-1-i]=1;
}

int max=0;
do
{
int add=0;
int all=0;
for(i=0;i<d;i++)
{
if(b[i])
all
+=1<<i;
}

for(i=0;i<n;i++)
{
if((all|a[i])==all)
add
++;
}

if(add>max)
max
=add;
}
while(next_permutation(&b[0],&b[d]));

printf(
"%d\n",max);
}
}