社区讨论

求助PASCAL

P1155[NOIP 2008 提高组] 双栈排序参与者 3已保存回复 4

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
4 条
当前快照
1 份
快照标识符
@mi4hlnrd
此快照首次捕获于
2025/11/18 19:25
4 个月前
此快照最后确认于
2025/11/18 19:25
4 个月前
查看原帖
PASCAL
program twosracks;
var
   n,t,should,i,j:longint;
   p,f,col:array[1..1500]of longint;
   pai:array[1..3,1..1010]of longint;
   l:array[1..3]of longint;
   wei,las,too:array[1..10010]of longint;
   function min(a,b:longint):longint;
   begin
     if a<b then exit(a) else exit(b);
   end;
  procedure line(x,y:longint);
  begin
    inc(t);
    las[t]:=wei[x];
    wei[x]:=t;
    too[t]:=y;
    inc(t);
    las[t]:=wei[y];
    wei[y]:=t;
    too[y]:=x;
  end;
  procedure draw(x,c:longint);
  var i:longint;
  begin
      if (col[x]=0) then col[x]:=c
                     else
                     if (col[x]<>c) then
                                    begin
                                      writeln(0);
                                      close(output);
                                      halt;
                                    end
                                    else exit;
     i:=wei[x];
     while (i<>0) do
     begin
       draw(too[i],3-c);
       i:=las[i];
     end;
  end;
begin
  assign(input,'stacks.in');
  assign(output,'stacks.out');
  reset(input);
  rewrite(output);
  readln(n);
  should:=1;
  t:=0;
  for i:=1 to n do
    read(p[i]);
  close(input);
  f[n]:=p[n];
  for i:=n-1 downto 1 do
     f[i]:=min(f[i+1],p[i]);
  pai[1,0]:=1002;
  col[n+1]:=1;
  p[n+1]:=1001;
  pai[2,0]:=1002;
  for i:=1 to n-2 do
    for j:=i+1 to n-1 do
        if (p[i]<p[j])and(f[j+1]<p[i])  then line(i,j);
  for i:=1 to n do
    if (col[i]=0) then draw(i,1);
  for i:=1 to n do
      write(col[i],' ');
  for i:=1 to n+1 do
  begin
    while true do
      if (pai[1][l[1]]=should) then
                               begin
                                  inc(should);
                                  dec(l[1]);
                                  write('b ');
                               end
                               else
      if (pai[i][l[2]]=should) then
                               begin
                                 inc(should);
                                 dec(l[2]);
                                 write('d ');
                               end
                               else break;
      inc(l[col[p[i]]]);
      pai[col[i]][l[col[i]]]:=p[i];
      if (i<n+1) then write(chr(2*col[i]+95),' ');
    end;
  close(output);
end. 

回复

4 条回复,欢迎继续交流。

正在加载回复...