比赛 20091112练习 评测结果 AAAAAAAAAA
题目名称 平衡的阵容 最终得分 100
用户昵称 王瑞祥K 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2009-11-12 11:23:12
显示代码纯文本
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.