Double Stack sort 2008 Noip National League Improvement Group (two-figure dyeing)

Source: Internet
Author: User

Double Stack sorting 

2008 Noip National League Improvement Group

time limit: 1 sspace limit: 128000 KBTopic rank: Master Master
  Title Description Description

Tom has been studying an interesting sort of problem recently. , with 2 stacks of S1 and s2,tom, I want to sort the input sequence in ascending order with the following 4 operation implementations.

Action A

If the input sequence is not empty, the first element is pressed into the stack S1

Action B

If the stack S1 is not empty, eject the top element of the S1 stack to the output sequence

Operation C

If the input sequence is not empty, the first element is pressed into the stack S2

Operation D

If the stack S2 is not empty, eject the top element of the S2 stack to the output sequence

If a 1~n arrangement p can be used to make the output sequence a series of ..., (n-1), N,tom is called P is a "double stack sort arrangement". For example (1,3,2,4) is a "double-stack sort sequence", and (2,3,4,1) is not. Describes an action sequence that sorts (1,3,2,4):<a,c,c,b,a,d,d,b>

Of course, this sequence of operations may have several, for the above example (1,3,2,4),<a,c,c,b,a,d,d,b> is another feasible sequence of operations. Tom wants to know what the sequence of operations with the smallest dictionary order is.

Enter a description input Description

The first line of input is an integer n.

The second line has n spaces separated by a positive integer, constituting a 1~n arrangement.

outputs description output Description

Output a total row, if the input arrangement is not "can be double-stack sort arrangement", the output number 0, otherwise the output dictionary order the smallest sequence of operations, each of the two operations separated by a space, there is no space at the end of the line.

sample input to sample

"Sample 1"

4

1 3 2 4

"Sample 2"

4

2 3 4 1

"Sample 3"

3

2 3 1

Sample output Sample outputs

"Sample 1"

A b A a b b a B

"Sample 2"

0

"Sample 3"

A c a b b d

data size & Hint

30% of the data meet: n<=10

50% of the data meet: n<=50

100% of the data meet: n<=1000

/*If you can not double-stack sequencing, there must be a conflict to the conflict with the edge, because there are only two stacks, determine whether the two-dimensional graph staining can consider the conflict. If there is i<j<k a[k]<a[i]<a[j] must not be a single-stack sort if i<j<k<x have a[x]<a[i]<a[j]<a[k] must not be double-stack sort preprocessing suffix minimum value, Connect the conflict with the edge. */#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<vector>#include<queue>#include<stack>#defineN 1007#defineINF 0x3f3f3f3f#defineB 1using namespacestd;intn,ans,cnt;intA[n],f[n],col[n],head[n];stack<int>s1,s2;structedge{intu,v,net;} E[n<<1];inlinevoidAddintUintv) {e[++cnt].v=v;e[cnt].net=head[u];head[u]=CNT;} InlineintRead () {intx=0, f=1;CharC=GetChar ();  while(c>'9'|| c<'0'){if(c=='-') f=-1; c=GetChar ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;}voidBFsintu) {Queue<int>q;q.push (u); col[u]=B;  while(!Q.empty ()) {        intnow=Q.front (); Q.pop ();  for(intI=head[now];i;i=e[i].net) {            intv=e[i].v; if(col[v]==-1) col[v]=col[now]^1, Q.push (v); Else if(Col[v]!= (col[now]^1) {printf ("0\n"); exit (0);} }    }}intMain () {memset (col,-1,sizeofcol); N=read ();  for(intI=1; i<=n;i++) a[i]=read (); F[n+1]=inf;  for(inti=n;i>=1; i--) F[i]=min (f[i+1],a[i]);  for(intI=1; i<=n;i++)       for(intj=i+1; j<=n;j++)        if(a[i]>f[j+1] && a[i]<A[j]) Add (i,j), add (j,i);  for(intI=1; i<=n;i++)       if(col[i]==-1) BFS (i); CNT=1;  for(intI=1; i<=n;i++)    {        if(col[i]==b) S1.push (A[i]), printf ("a"); ElseS2.push (A[i]), printf ("C");  while((!s1.empty () && s1.top () ==cnt) | | (!s2.empty () && s2.top () = =CNT)) {            if(!s1.empty () && s1.top () ==cnt) S1.pop (), printf ("b"); ElseS2.pop (), printf ("D"); ++CNT; }    }    return 0;}

Double Stack sort 2008 Noip National League Improvement Group (two-figure dyeing)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.