记录编号 39471 评测结果 AAAAAAAAAAAA
题目名称 平衡奶牛 最终得分 100
用户昵称 Gravatarfuhao 是否通过 通过
代码语言 Pascal 运行时间 0.751 s
提交时间 2012-07-11 19:49:55 内存使用 94.01 MiB
显示代码纯文本
program Balline;
const maxn=100001; yu=3000001; fhash=1000731; maxk=31; maxm=3000001;
var
 sum:array[0..maxn,0..maxk] of int64; d:array[0..maxk] of int64;
 a:array[0..yu] of int64;
 id:array[0..maxn] of int64;
 pos,next:array[0..maxm] of int64;
 tot,n,k,i,j,s,t,ans:longint;
 function same(l,r:longint):boolean;
 var i:longint;
 begin
  for i:=1 to k do
   if sum[l,i]<>sum[r,i] then exit(false);
  same:=true;
 end;

 function hash(v:longint):longint;
 var i:Longint;  t:int64;
 begin
  t:=0;
  for i:=1 to k do
    t:=(t+(yu+sum[v,i]) mod yu*d[i]) mod yu * fhash mod yu;
  i:=a[t];
  while i<>0 do
   begin
    if same(pos[i],v) then exit(v-pos[i]);
    i:=next[i];
   end;
  inc(tot); next[tot]:=a[t]; a[t]:=tot; pos[tot]:=v;
  if id[v]=1 shl k-1 then exit(1) else exit(0);
 end;

begin
  assign(input,'balline.in'); reset(input);
  assign(output,'balline.out'); rewrite(output);
  readln(n,k);
  for i:=1 to k do d[i]:=1 shl (i-1);
  for i:=1 to n do
   begin
    readln(s);
    id[i]:=s;
    for j:=1 to k do
      if d[j] and s=d[j] then
       sum[i,j]:=sum[i-1,j]+1 else
       sum[i,j]:=sum[i-1,j];
   end;
  for i:=1 to n do
   begin
    t:=sum[i,1];
    for j:=1 to k do sum[i,j]:=sum[i,j]-t;
   end;
  for i:=0 to n do
   begin
    t:=hash(i);
    if ans<t then ans:=t;
   end;
  writeln(ans);
  close(input); close(output);
end.