比赛 20120711 评测结果 AAAAAAAAAAAA
题目名称 平衡奶牛 最终得分 100
用户昵称 SnowDancer 运行时间 0.570 s
代码语言 Pascal 内存使用 37.63 MiB
提交时间 2012-07-11 11:48:56
显示代码纯文本
program balline;
const
  base=1717173;
type
  point=^node;
  node=record pos:longint;next:point; end;
var
  code,id:array[0..100000,1..30] of longint;
  nodes:array[1..3000000] of point;
  h:array[0..base] of point;
  n,i,j,k,l,ans:longint;
  top,p,t:point;
  hash:int64;
function equal(x,y:longint):boolean;
  var
    i:longint;
  begin
    for i:=2 to k do
      if id[x,i]<>id[y,i] then
        exit(false);
    exit(true);
  end;
procedure insert(hash,i:longint);
  begin
    p:=h[hash];
    while p<>nil do begin
      if equal(p^.pos,i) then begin
        if i-p^.pos>ans then
          ans:=i-p^.pos;
        exit;
      end;
      p:=p^.next;
    end;
    top^.pos:=i;top^.next:=h[hash];
    h[hash]:=top; inc(top);
  end;
begin
assign(input,'balline.in');reset(input);
assign(output,'balline.out');rewrite(output);
  readln(n,k);   top:=@nodes[1];
  for i:=1 to n do begin
    readln(l);
    for j:=1 to k do begin
      code[i,j]:=(l>>(j-1)) and 1;
      inc(code[i,j],code[i-1,j]);
    end;
  end;
  for i:=0 to n do begin
    hash:=0;
    for j:=2 to k do begin
      id[i,j]:=code[i,j]-code[i,j-1];
      hash:=(hash*13137+id[i,j]) mod base;
    end;
    insert(hash,i);
  end;
  writeln(ans);
close(input);close(output);
end.