比赛 20120711 评测结果 WAWWWWWWWWWW
题目名称 平衡奶牛 最终得分 8
用户昵称 fuhao 运行时间 0.894 s
代码语言 Pascal 内存使用 12.37 MiB
提交时间 2012-07-11 11:54:47
显示代码纯文本
const maxk=30; maxn=100001;
var
 n,k,i,j,min,last,r,l,mid,ans:longint;
 sum:array[0..maxn,0..maxk] of longint;
 id:array[0..maxn] of longint;
 d:array[0..maxk] of longint;
 flag:boolean;
begin
 assign(input,'balline.in'); reset(input);
 assign(output,'balline.out'); rewrite(output);
 readln(n,k);
 for i:=1 to n do readln(id[i]);
 for j:=1 to k do d[j]:=1 shl (j-1);
 for i:=1 to n do
 begin
  sum[i,0]:=maxlongint div 2;
  for j:=1 to k do
   if d[j] and id[i]=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
   l:=1; r:=i; last:=0;
   for j:=1 to k do
    if sum[i,j]<sum[i,last] then last:=j;
   while r-l>1 do
    begin
     mid:=(l+r) shr 1;
     min:=0;
     for j:=1 to k do
      begin
      if min=0 then min:=j;
      if sum[i,j]-sum[mid-1,j]<
         sum[i,min]-sum[mid-1,min] then
          min:=j;
      end;
     if min<>last then r:=mid else l:=mid;
     last:=min;
    end;
   min:=sum[i,k]-sum[l-1,k];
   flag:=true;
   for j:=1 to k do
    if min<>sum[i,k]-sum[l-1,k] then
     begin flag:=false; break; end;
   if flag then if i-l+1>ans then ans:=i-l+1;
   flag:=true;
   min:=sum[i,k]-sum[r-1,k];
   for j:=1 to k do
    if min<>sum[i,k]-sum[l-1,k] then
     begin flag:=false; break; end;
   if flag then if i-r+1>ans then ans:=i-r+1;
  end;
 writeln(ans);
 close(input); close(output);
end.