比赛 20091112练习 评测结果 MMMMMMMMMM
题目名称 平衡的阵容 最终得分 0
用户昵称 い夢£神话︷ 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2009-11-12 10:45:13
显示代码纯文本
program aa;
type
        son=record
                zuo,you:longint;
                zho:array[0..1]of longint;
        end;
var
        cow:array[1..50000,1..2]of longint;
        t:text;
        tu:array[1..5000,1..5000]of son;
        loop,loop1,zero,one,n:longint;
        ans,long:longint;
procedure swap(var x,y:longint);
var
        u:longint;
begin
        u:=x;
        x:=y;
        y:=u;
end;
procedure qsort(l,r:longint);
var
        i,j:longint;
        mid:longint;
begin
        i:=l;j:=r;mid:=cow[(i+j) div 2,2];
        repeat
                while cow[i,2]<mid do
                        inc(i);
                while cow[j,2]>mid do
                        dec(j);
                if i<=j then
                begin
                        swap(cow[i,2],cow[j,2]);
                        swap(cow[i,1],cow[j,1]);
                        inc(i);
                        dec(j);
                end;
        until i>j;
        if l<j then qsort(l,j);
        if i<r then qsort(i,r);
end;
begin
        zero:=0;
        one:=0;
        assign(t,'balance.in');
        reset(t);
        readln(t,n);
        for loop:=1 to n do
        begin
                readln(t,cow[loop,1],cow[loop,2]);
                        if cow[loop,1]=1 then
                                inc(one)
                        else
                                inc(zero);
        end;
        qsort(1,n);
        long:=cow[n,2]-cow[1,2];
        if one=zero then
                ans:=long
        else
        begin
        for loop:=1 to n do
        begin
                tu[1,loop].zuo:=cow[loop,2];
                tu[1,loop].you:=cow[loop,2];
                inc(tu[1,loop].zho[cow[loop,1]]);
        end;
        for loop:=2 to n do
                for loop1:=1 to n-loop+1 do
                begin
                        tu[loop,loop1].zuo:=cow[loop1,2];
                        tu[loop,loop1].you:=cow[loop1+loop-1,2];
                        tu[loop,loop1].zho:=tu[loop-1,loop1].zho;
                        inc(tu[loop,loop1].zho[cow[loop1+loop-1,1]]);
                        if tu[loop,loop1].zho[0]=tu[loop,loop1].zho[1] then
                                if tu[loop,loop1].you-tu[loop,loop1].zuo>ans then
                                        ans:=tu[loop,loop1].you-tu[loop,loop1].zuo;
                end;
        end;
        assign(t,'balance.out');
        rewrite(t);
        writeln(t,ans);
        close(t);
end.