比赛 20120711 评测结果 AAAAAAAAATTT
题目名称 平衡奶牛 最终得分 75
用户昵称 zhangchi 运行时间 3.049 s
代码语言 Pascal 内存使用 0.54 MiB
提交时间 2012-07-11 09:41:32
显示代码纯文本
var
  n,k,i,j:longint;
  bit:array[1..32] of longint;
  a:array[1..100000] of longint;
  function work(x:longint):boolean;
  var
    i,j:longint;
    q:boolean;
  begin
    fillchar(bit,sizeof(bit),0);
    q:=false;
    for i:=1 to x do
      for j:=1 to k do
        inc(bit[j],(a[i] shr (j-1)) and 1);
    for i:=1 to k-1 do
      if bit[i]<>bit[i+1] then break;
    if (i=k-1)and(bit[k-1]=bit[k]) then q:=true;
    for i:=x+1 to n do
      begin
        for j:=1 to k do
          dec(bit[j],(a[i-x] shr (j-1))and 1);
        for j:=1 to k do
          inc(bit[j],(a[i] shr (j-1))and 1);
        for j:=1 to k-1 do
          if bit[j]<>bit[j+1] then break;
        if (j=k-1)and(bit[k-1]=bit[k]) then begin q:=true; break; end;
      end;
    work:=q;
  end;
begin
  assign(input,'balline.in'); reset(input);
  assign(output,'balline.out'); rewrite(output);
  readln(n,k);
  for i:=1 to n do
    readln(a[i]);
  for i:=n downto 1 do
    begin
      if work(i) then
        begin
          writeln(i);
          close(input); close(output);
          halt;
        end;
    end;
  writeln(0);
  close(input); close(output);
end.