记录编号 15365 评测结果 AAAAAAAAAA
题目名称 [USACO Mar07] 平衡的阵容 最终得分 100
用户昵称 Gravatar王瑞祥K 是否通过 通过
代码语言 Pascal 运行时间 0.182 s
提交时间 2009-11-12 16:11:01 内存使用 1.07 MiB
显示代码纯文本
program balance(input,output);
var
 a,sum,b:array[0..50000] of longint;
 hash:array[-50000..50000] of longint;
 i,j,k,m,n,ans:longint;
procedure qsort(r,l:longint);
var i,j,x,y:longint;
begin
 i:=r; j:=l; x:=b[(i+j) div 2];
repeat
 while b[i]<x do inc(i);
 while b[j]>x do dec(j);
 if i<=j then begin
  y:=a[i]; a[i]:=a[j]; a[j]:=y;
  y:=b[i]; b[i]:=b[j]; b[j]:=y;
  inc(i); dec(j);
  end;
 until i>j;
 if i<l then qsort(i,l);
 if j>r then qsort(r,j);
end;
begin
 assign(input,'balance.in');assign(output,'balance.out');
 reset(input); rewrite(output);
 readln(n);
 for i:=-n to n do hash[i]:=0;
 for i:=1 to n do readln(a[i],b[i]);
 qsort(1,n);
 sum[0]:=0;
 for i:=1 to n do
  if a[i]=0 then sum[i]:=sum[i-1]-1 else sum[i]:=sum[i-1]+1;
 hash[0]:=0;
 for i:=1 to n do
  if (hash[sum[i]]=0)and(sum[i]<>0) then hash[sum[i]]:=i;
 ans:=0;
 for i:=1 to n do
  if (b[i]-b[hash[sum[i]]+1]>ans)and(i>hash[sum[i]]) then
   ans:=b[i]-b[hash[sum[i]]+1];
 writeln(ans);
 close(input);close(output);
end.