http://codevs.cn/problem/1170/(Topic link)
Test instructions
An initial sequence is given to determine whether an ordered sequence can be constructed by stacking and stacking operations of two stacks. If possible, output a scheme with the smallest dictionary order.
Solution
Still want to sniper LCF only to see this problem, really is very god ah. One of the problems that the examination room absolutely cannot do.
Online a big Piang, that conclusion is actually very good yy out, just can't think of conversion to the binary graph dyeing above go.
Code
codevs1170#include<algorithm> #include <iostream> #include <cstdlib> #include <cstring># include<cstdio> #include <cmath> #define LL long long#define MOD 100000000#define inf 2147483640#define Pi ACOs ( -1.0) #define FREE (a) freopen (a ".", "R", stdin), Freopen (a ". Out", "w", stdout); using namespace Std;const int maxn= 1010;struct Edge {int to,next;} E[maxn<<1];int c[maxn],head[maxn],a[maxn],f[maxn],s1[maxn],s2[maxn];int t1,t2,n,cnt;void Link (int u,int v) {e[ ++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;e[++cnt].to=u;e[cnt].next=head[v];head[v]=cnt;} BOOL Color (int x,int cl) {c[x]=cl;for (int i=head[x];i;i=e[i].next) {if (!c[e[i].to]) {if (!color (E[I].TO,3-CL)) return 0; }else if (C[E[I].TO]==CL) return 0;} return 1;} int main () {scanf ("%d", &n), for (int i=1;i<=n;i++) scanf ("%d", &a[i]), f[n+1]=inf;for (int i=n;i;i--) f[i]=min (F[i+1],a[i]); 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]) Link (i,j ); for (int i=1;i<=n;i++) if (!c[i] &&!color (i,1)) {puts ("0"); return 0;} int now=1;for (int i=1;i<=n;i++) {if (c[i]==1) printf ("a"), S1[++t1]=a[i];else printf ("C"), S2[++t2]=a[i];while (s1[ T1]==now | | S2[t2]==now) {if (S1[t1]==now) printf ("B"), T1--;else printf ("D"), t2--;now++;}} return 0;}
"codevs1170" Double stack Sort