noip2008 Double Stack Sort

Source: Internet
Author: User

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>

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.

Output 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.

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.

I will not write the Noip topic to fill up ~ actually now I still do not.

Feel this problem thinking very God ... First of all, the greedy choice is obviously wrong. And then...... What the hell to do with this problem ah Ah!

So in the end I still went to see the puzzle ...

This question first to prove a conclusion: for any two number AI and aJ , they can not be pressed into the same stack (not only in the stack, but in the same stack) the necessary and sufficient conditions are: there is a k, so that i<j<k and Ak<ai<aj .

First of all to prove adequacy. Because of the Ak<ai, when AK is in the stack, AJ will be under the AI, so there is no solution.

Then prove the necessity. This thing is more difficult to prove, so we can turn to demonstrate its inverse of the proposition: if for any i<j<k, do not meet the Ak<ai<aj, then they can press into a stack.

There are two cases of not satisfying Ak<ai<aj: one is Ak>ai, the other is Ai>aj.

For the first case, because of the Ak>ai, the AI can already eject before the AK is in the stack, so it can be pressed into the same stack.

For the second case, it is clear that regardless of the size of the AK, the AI and AJ can be pressed into the same stack first, and AJ will pop up before the AI.

So the inverse of the proposition is to be proven, so the original proposition.

So the question becomes a two-point figure. Can not be pressed into the same stack point edge, and then determine that a graph is not a binary diagram without solution, otherwise there is a solution.

Then there is the problem of the smallest dictionary order. Due to the minimum number of dictionaries required to the first stack as far as possible to lose, so you can judge from the front to the back, in the binary map of the DFS again.

So this problem is so happy to solve spicy! can I really write this question in the examination room?

Summary: 1. When you encounter a kind of problem with no thinking, you should push the question to see if there is any wonderful nature;

2. When you find that your complexity is far too low, be sure to check it more than once.

3. Be sure to write the violence first and use it to verify your own guesses.

Paste the following code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath> #define FILE (s) freopen (S ".", "R", stdin), Freopen (S ". Out", "w", stdout) #define N 1010using namespace std; typedef long LONG Llg;int n,a[n<<1],wa[n],co[n],b[n],lb;int head[n],next[n*n],to[n*n],tt;int s1[N],t1,s2[N],t2 , Now=1;bool vis[n];int getint () {int w=0;bool q=0;char C=getchar (); while (c> ' 9 ' | | c< ' 0 ') &&c!= '-') C=getchar (), if (c== '-') C=getchar (), Q=1;while (c>= ' 0 ' &&c<= ' 9 ') w=w*10+c-' 0 ', C=getchar (); return q?-w:w;} void link (int x,int y) {to[++tt]=y;next[tt]=head[x];head[x]=tt;to[++tt]=x;next[tt]=head[y];head[y]=tt;} BOOL Ran (int u) {bool Ww=0;for (int i=head[u],v;v=to[i],i;i=next[i]) if (Co[v]==co[u]) return 0;else if (!co[v]) Co[v]=3-co [U],ww|=!ran (v); return!WW;} void Dfs (int u,bool w) {if (w) co[u]=3-co[u]; vis[u]=1;for (int i=head[u],v;v=to[i],i;i=next[i]) if (!vis[v]) DFS (V,W);} int main () {File ("a"); N=getint (); wa[n+1]=2147483647;for (int i=1;i<=n;i++) A[I]=getint (); for (int i=n;i>=1;i--) wa[i]=min (Wa[i+1],a[i]), for (Int. i=1;i<n;i++) for (int j=i+1;j<n;j++) if (a [I]<a[j] && a[i]>wa[j+1]) Link (i,j); for (int i=1;i<=n;i++) if (!co[i]) {co[i]=1;if (!ran (i)) {printf ("0 "); return 0;}} for (int i=1;i<=n;i++) if (!vis[i]) DFS (i,co[i]!=1), for (int i=1;i<=n;i++) {if (co[i]==1) s1[++t1]=a[i],b[++lb]=1; if (co[i]==2) s2[++t2]=a[i],b[++lb]=3;while (S1[t1]==now | | s2[t2]==now) {if (s1[t1]==now) T1--, b[++lb]=2;if (s2[t2]== Now) T2--, b[++lb]=4;now++;}} for (int i=1;i<=lb;i++) printf ("%c", ' a ' +b[i]-1);}

Noip2008 double-stack sort

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.