记录编号 15361 评测结果 AAAAAAAAAA
题目名称 [USACO Mar07] 平衡的阵容 最终得分 100
用户昵称 Gravatarbing 是否通过 通过
代码语言 Pascal 运行时间 0.184 s
提交时间 2009-11-12 15:50:51 内存使用 0.87 MiB
显示代码纯文本
program bing;
type
 sb=record
 a:integer;
 b:longint;
end;
var
 f1,f2:text;
 n:longint;
 s:array[1..50000] of sb;
 hash:array[-50000..50000] of longint;
procedure init;
var
 i:longint;
begin
 assign(f1,'balance.in');reset(f1);
 assign(f2,'balance.out');rewrite(f2);
 readln(f1,n);
 for i:=1 to n do
  readln(f1,s[i].a,s[i].b);
end;
procedure kp(l,r:longint);
var
 i,j:longint;
 t:sb;
 x:longint;
begin
 i:=l;j:=r;
 x:=s[(i+j)shr 1].b;
 repeat
  while s[i].b<x do inc(i);
  while s[j].b>x do dec(j);
  if i<=j then
  begin
   t:=s[i];s[i]:=s[j];s[j]:=t;
   inc(i);dec(j);
  end;
 until i>j;
 if l<j then kp(l,j);
 if i<r then kp(i,r);
end;
procedure wokao;
var
 i,max:longint;
 sum:array[0..50000] of longint;
begin
 max:=0;
 fillchar(hash,sizeof(hash),0);
 sum[0]:=0;
 for i:=1 to n do
 begin
  if s[i].a=0 then sum[i]:=sum[i-1]-1 else sum[i]:=sum[i-1]+1;
  if hash[sum[i]]=0 then hash[sum[i]]:=i;
 end;
 for i:=1 to n do
 begin

  if (i>1+hash[sum[i]])and(s[i].b-s[1+hash[sum[i]]].b>max) then max:=s[i].b-s[1+hash[sum[i]]].b;
 end;
 writeln(f2,max);
 close(f1);close(f2);
end;
begin
 init;
 kp(1,n);
 wokao;
end.