比赛 2008haoi模拟训练3 评测结果 AAAAWAAAAT
题目名称 阶梯教室设备利用 最终得分 80
用户昵称 梦里醉逍遥 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-04-24 09:51:47
显示代码纯文本
program rez;
const
  fi='rez.in';  fo='rez.out';
  maxn=10000;  maxk=30000;
type
  arr1=array[-1..maxn] of longint;
  arr2=array[0..maxk] of longint;
var
  fin,fout:text;  n:longint;
  s,t:arr1;  f,a:arr2;

  procedure swap(var i,j:longint);
  var
    tmp:longint;
  begin
    tmp:=i;  i:=j;  j:=tmp;
  end;

  procedure qsort(i,j:longint);
  var
    l,r,mid:longint;
  begin
    l:=i;  r:=j;  mid:=t[(i+j) shr 1];
    repeat
      while t[l]<mid do l:=l+1;
      while t[r]>mid do r:=r-1;
      if l<=r then begin
        swap(s[l],s[r]);
        swap(t[l],t[r]);
        l:=l+1;  r:=r-1;
      end;
    until l>r;
    if r>i then qsort(i,r);
    if j>l then qsort(l,j);
  end;

  procedure init;
  var
    i:longint;
  begin
    assign(fin,fi);  reset(fin);
    readln(fin,n);
    for i:=1 to n do
      readln(fin,s[i],t[i]);
    close(fin);
    qsort(1,n);
  end;

  procedure main;
  var
    i,j:longint;
  begin
    f[t[1]]:=t[1]-s[1];
    for i:=1 to n do a[t[i]]:=-1;
    for i:=2 to n do begin
      j:=i-1;
      while (t[j]>a[t[i]]) and (j>=0) do begin
        if (f[t[j]]+t[i]-s[i]>f[t[i]]) and (s[i]>=t[j]) then begin
          f[t[i]]:=f[t[j]]+t[i]-s[i];
          a[t[i]]:=s[j];
        end;
        j:=j-1;
      end;
    end;
  end;

  procedure print;
  var
    i,max:longint;
  begin
    max:=0;
    for i:=n downto 1 do
      if f[t[i]]>max then max:=f[t[n]];
    assign(fout,fo);  rewrite(fout);
    writeln(fout,max);  close(fout);
  end;

begin
  init;
  main;
  print;
end.