[Daniel is a cow] double-stack sorting

Source: Internet
Author: User

[Description]

Tom is studying an interesting sorting problem recently ., With two stacks S1 and S2, Tom wants to sort input sequences in ascending order using the following four operations.

Operation
If the input sequence is not empty, press the first element into Stack S1.
Operation B
If Stack S1 is not empty, the top element of stack S1 is popped up to the output sequence.
Operation C
If the input sequence is not empty, press the first element into Stack S2.
Operation d
If Stack S2 is not empty, the top element of stack S2 is displayed to the output sequence.

If ~ The arrangement of n p allows the output sequence to be 1, 2 ,..., (N-1), N, Tom said P is a "double stack sorting ". For example, (1, 3, 2, 4) is a "dual-stack sorting sequence", and (2, 3, 4, 1) is not. Describes an operation sequence for sorting (1, 3, 2, 4): <A, C, C, B, A, D, D, B>

Of course, there may be several such operation sequences. For the above example (1, 3, 2, 4), <A, C, C, B, A, D, D, b> is another feasible operation sequence. Tom wants to know what the operation sequence with the smallest Lexicographic Order is.

[Input format]

The first line of the input is an integer n. The second line contains n positive integers separated by spaces, forming a 1 ~ N.

[Output format]

The output is a row in total. If the input is not sorted by double stacks, the number 0 is output; otherwise, the operation sequence with the smallest Lexicographic Order is output, which is separated by spaces, there is no space at the end of the line.

[Input example]

3
2 3 1
[Output example]

A C A B D
[Analysis]

Let's take a look at sqybi's topic:Http://sqybi.com/blog/archives/78

 

# Include <stdio. h ># include <iostream> # define maxn 1010 using namespace STD; int A [maxn], Zhan [3] [maxn], color [maxn], F [maxn]; int now, N; bool wujie; struct tnode {int num; tnode * Next;} G [maxn], * t; void insert (int x, tnode & P) {T = new (tnode); t-> num = x; t-> next = P. next; p. next = T;} void DFS (int x) {tnode * tt; TT = G [X]. next; while (TT! = NULL) {int y = tt-> num; If (! Color [y]) {color [y] = 3-color [X]; DFS (y);} If (color [y] = color [x]) {wujie = 1; return;} TT = tt-> next ;}} int main () {scanf ("% d", & N); For (INT I = 1; I <= N; ++ I) scanf ("% d", & A [I]); F [n + 1] = 1000000000; For (INT I = N; i> 0; -- I) f [I] = min (A [I], F [I + 1]); For (INT I = 1; I <= N; ++ I) for (Int J = I + 1; j <= N; ++ J) if (A [I] <A [J]) & (f [J + 1] <A [I]) {insert (J, G [I]); insert (I, G [J]);} (INT I = 1; I <= N; ++ I) if (! Color [I]) {color [I] = 1; DFS (I); If (wujie) break;} If (wujie) {printf ("0 \ n "); return 0;} Now = 1; for (INT I = 1; I <= N; ++ I) {int x = color [I]; zhan [x] [++ Zhan [x] [0] = I; If (x = 1) printf ("A"); else printf ("C "); while (A [Zhan [1] [Zhan [1] [0] = now) | (A [Zhan [2] [Zhan [2] [0] = now )) {if (a [Zhan [1] [Zhan [1] [0] = now) {printf ("B "); -- Zhan [1] [0];} else {printf ("D"); -- Zhan [2] [0] ;}++ now ;}}}

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.