I and OI
Past...

题意:有N头奶牛,D种疾病,已知每头奶牛都得了哪几种疾病.现在要选出尽可能多的奶牛,使得这些奶牛所带的疾病种类不大于K种,问最多能选几只.

分析:D<=15,N<=1000,考虑暴力枚举疾病种类的组合,O(N)求出可以入选的奶牛数.

因为有K的限制,枚举的复杂度最多为C(D,D/2)只有6000多.位运算优化下,居然79MS.

code:

var   dis:array[0..1001] of longint;
      n,d,k,i,j,l,di,tmp,ans:longint;

      function lowbit(x:longint):longint;
      begin
            exit(x and (x xor (x-1)));
      end;

      function count(x:longint):longint;
      begin
            count:=0;
            while x>0 do
            begin
                  inc(count);
                  dec(x,lowbit(x));
            end;
      end;

begin

      readln(n,d,k);
      for i:=1 to n do
      begin
            read(di);
            for j:=1 to di do
            begin
                  read(l);
                  dis[i]:=dis[i]+1<<(l-1);
            end;
            readln;
      end;

      for di:=0 to 1<<d-1 do
      if count(di)=k then
      begin
            tmp:=0;
            for i:=1 to n do
               if dis[i] or di=di then inc(tmp);
            if tmp>ans then ans:=tmp;
      end;
      writeln(ans);

end.
posted on 2011-08-12 17:32  exponent  阅读(204)  评论(0编辑  收藏  举报