Description
Tom is recently studying an interesting sort of problem. As shown in the figure, through the 2 stacks S1 and S2,tom want to use the following 4 operations to achieve the order of the input sequence ascending sort.
Operation A
If the input sequence is not empty, press the first element into the stack S1
Operation b
If the stack S1 is not empty, eject the S1 stack top element to the output sequence
Operation C
If the input sequence is not empty, press the first element into the stack S2
Operation D
If the stack S2 is not empty, eject the S2 stack top element to the output sequence
If a 1~n arrangement p can be made by a series of operations to make the output sequence 1,2, ..., (n-1), N,tom 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. The following figure describes an action sequence that sorts (1,3,2,4):< a,c,c,b,a,d,d,b>
Of course, there may be several such sequences of operations, and 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 in the dictionary sequence is the smallest. Input Format
The first line of the input file twostack.in is an integer n. The second row has n a positive integer separated by a space, which forms a 1~n arrangement Output Format
Output file Twostack.out A total row, if the input arrangement is not "can double stack sort arrangement", output number 0, otherwise output dictionary order of the smallest sequence of operations, every two operations separated by a space, line end without space Sample Input
"Input Sample 1"
4
1 3 2 4
"Input Sample 2" 4 2 3 4 1
"Input Sample 3"
3 2 3 1
Sample Output
Output Sample 1
A B a b b A B
output sample 2
0
Output Example 3
A c a b b d
Hint
"Limit" 30% data satisfaction: n<=10
50% Data satisfaction: n<=50
100% Data satisfaction: n<=1000
Exercises
Personally think the idea is a little difficult (anyway I can't imagine)
Binary graph dyeing + simulation
The first pair of each pair can never enter the same stack of two number of build edge (if there is K, meet i<j<k, make s[k]<s[i]<s[j] then i,j can never be the same stack)
The graph is stained with a binary graph (so that the two number of contradictions can be divided into two stacks) if it's a binary graph, it can be sorted by two stacks (then the output is simulated)
When dyeing, increase by number to find the point of the original color is 0, so that the dictionary order can be guaranteed
See Code for details
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < string> #include <cstdio> #include <queue> #include <ctime> #include <cmath> #include <
Vector> using namespace std;
int i,j,k,l,m,n,pd,b;
int a[1005],col[1005],s[3][1005],num[3];
Vector <int> road[1005];
void work (int u) {if (pd==1) return;
for (int i=0;i<road[u].size (); i++) {int v=road[u][i];
if (!col[v]) col[v]=3-col[u],work (v);
if (Col[v]!=3-col[u]) {B=0;pd=1;return;}
int main () {scanf ("%d", &n);
for (i=1;i<=n;i++) scanf ("%d", &a[i]);
K=1e9;
for (j=n;j;j--) {k=min (k,a[j]);
for (i=1;i<=j-1;i++) if (a[i]<a[j]&&a[i]>k) Road[i].push_back (j), Road[j].push_back (i);
} b=1;
for (i=1;i<=n;i++) {if (!col[i]) col[i]=1,pd=0,work (i);
if (b==0) {printf ("0"); return 0;}
for (j=i=1;i<=n;i++) {if (col[i]==1) printf ("a"); else printf ("C");
S[col[i]][++num[col[i]]]=i; for (; a[s[1][num[1]]]==j| |
a[s[2][num[2]]]==j;j++) {if (a[s[1][num[1]]]==j) printf ("B"),--num[1];
else printf ("D"),--num[2]; }
}
}