记录编号 15391 评测结果 AAAAAAAAAA
题目名称 [USACO Mar07] 平衡的阵容 最终得分 100
用户昵称 GravatarZhouZn1 是否通过 通过
代码语言 Pascal 运行时间 0.186 s
提交时间 2009-11-12 17:59:34 内存使用 1.16 MiB
显示代码纯文本
program balance;
type
        rec=record
        id,x:longint;
        end;
var
        n,i,j,ans:longint;
        data:array[1..50000]of rec;
        f:array[0..50000]of longint;
        hash:array[-50000..50000]of longint;
        v:array[-50000..50000]of boolean;
procedure swap(var a,b:rec);
var
        t:rec;
begin
        t:=a;
        a:=b;
        b:=t;
end;
procedure qsort(l,r:longint);
  var i,j,mid:longint;
  begin
    i:=l;j:=r; mid:=data[(l+r) div 2].x;
  repeat
    while data[i].x<mid do inc(i);
    while data[j].x>mid do dec(j);
    if i<=j then begin
    swap(data[i],data[j]);
    inc(i);dec(j);
    end;
  until i>j;
  if l<j then qsort(l,j);
  if i<r then qsort(i,r);
end;{sort}
procedure init;
begin
        assign(input,'balance.in');
        reset(input);
        assign(output,'balance.out');
        rewrite(output);
        readln(n);
        for i:=1 to n do
        begin
                readln(data[i].id,data[i].x);
                if data[i].id=0 then data[i].id:=-1;
        end;
        qsort(1,n);
        f[0]:=0;
        if data[1].id=1 then f[1]:=1 else f[1]:=0;
        fillchar(v,sizeof(v),0);
        for i:=2 to n do
         begin
         f[i]:=f[i-1]+data[i].id;
         if not(v[f[i]]) then
         begin
                v[f[i]]:=true;
                hash[f[i]]:=i;
         end;
         end;
end;
procedure closef;
begin
        close(input);
        close(output);
end;
procedure main;
var
        t:longint;
begin
        ans:=-1;
        hash[0]:=1;
        for i:=1 to n do
           begin
           t:=hash[f[i]];
           if i>=t+1 then
            if data[i].x-data[t+1].x>ans then ans:=data[i].x-data[t+1].x;
           end;
        writeln(ans);

end;
begin
        init;
        main;
        closef;
end.