记录编号 8253 评测结果 AAAAAAAAAAW
题目名称 [BYVoid S3] 彩色穿孔卡片 最终得分 90
用户昵称 Gravatarzhai 是否通过 未通过
代码语言 Pascal 运行时间 2.559 s
提交时间 2008-11-13 13:23:50 内存使用 0.28 MiB
显示代码纯文本
program punch;
  const
    max=20010;
  type
    sz=array[1..2,1..max]of longint;
    sx=array[0..max]of boolean;
  var
    c:sz;
    f1,f2:text;
    n:integer;
    i,j1,j2,h,t:integer;
    ans:longint;
    a,b:longint;
    flag:sx;
    procedure zhao(var h:integer;x:longint;var j:integer);
      var
        k:integer;
        t1:integer;
      begin
        t1:=t;
        while (h<>t1)do begin
          k:=(h+t1)div 2;
          if x>c[1,k] then h:=k+1
          else t1:=k;
        end;
        j:=h;
        if x>c[1,h] then inc(j);
      end;
    procedure pai(q:integer);
      var
        i:integer;
      begin
        zhao(h,a,j1);
        if a<>c[1,j1] then begin
          for i:=t downto j1 do begin
            c[1,i+1]:=c[1,i];
            c[2,i+1]:=c[2,i];
          end;
          inc(t);
          c[1,j1]:=a;
        end;
        zhao(h,b,j2);
        if b<>c[1,j2] then begin
          for i:=t downto j2 do begin
            c[1,i+1]:=c[1,i];
            c[2,i+1]:=c[2,i];
          end;
          inc(t);
          c[1,j2]:=b;
        end;
        for i:=j1 to j2 do c[2,i]:=q;
      end;
  begin
    assign(f1,'punch.in');reset(f1);
    assign(f2,'punch.out');rewrite(f2);
    fillchar(c,sizeof(c),0);
    fillchar(flag,sizeof(flag),false);
    j1:=0;t:=2;
    j2:=0;
    readln(f1,n);
    readln(f1,c[1,1],c[1,2]);
    c[2,1]:=1;c[2,2]:=1;
    for i:=2 to n do begin
      readln(f1,a,b);
      h:=1;
      pai(i);
    end;
    ans:=0;
    for i:=1 to t do flag[c[2,i]]:=true;
    for i:=1 to n do if flag[i] then inc(ans);
    write(f2,ans);
    close(f1);
    close(f2);
  end.