(twostack.pas/c/cpp)
【问题描述】
Tom最近在研究一个有趣的排序问题。如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序。
操作a
如果输入序列不为空,将第一个元素压入栈S1
操作b
如果栈S1不为空,将S1栈顶元素弹出至输出序列
操作c
如果输入序列不为空,将第一个元素压入栈S2
操作d
如果栈S2不为空,将S2栈顶元素弹出至输出序列
如果一个1~n的排列P可以通过一系列操作使得输出序列为1,2,…,(n-1),n,Tom就称P是一个“可双栈排序排列”。例如(1,3,2,4)就是一个“可双栈排序序列”,而(2,3,4,1)不是。下图描述了一个将(1,3,2,4)排序的操作序列:<a,c,c,b,a,d,d,b>
当然,这样的操作序列有可能有几个,对于上例(1,3,2,4),<a,c,c,b,a,d,d,b>是另外一个可行的操作序列。Tom希望知道其中字典序最小的操作序列是什么。
【输入】
输入文件twostack.in的第一行是一个整数n。
第二行有n个用空格隔开的正整数,构成一个1~n的排列。
【输出】
输出文件twostack.out共一行,如果输入的排列不是“可双栈排序排列”,输出数字0;否则输出字典序最小的操作序列,每两个操作之间用空格隔开,行尾没有空格。
【输入输出样例1】
twostack.in |
twostack.out |
4 1 3 2 4 |
a b a a b b a b |
【输入输出样例2】
twostack.in |
twostack.out |
4 2 3 4 1 |
0 |
【输入输出样例3】
twostack.in |
twostack.out |
3 2 3 1 |
a c a b b d |
【限制】
30%的数据满足: n<=10
50%的数据满足: n<=50
100%的数据满足: n<=1000
【分析】:
首先,若满足条件“存在data[i]<data[j]且在j之后存在data[k]<data[i]”<元素i进入A记为β,进B记为β'>,那么将那么i和j'必须分配在同一组,i'和j必须分配在同一组<分组就用并查集实现>,然后搜若某数i和i'在同一组,则输出0,否则染色:枚举1-N中的数,若该数未染色,染色,然后若有1-N的数与之在同一组,那么染成同一颜色,若N+1-2N范围且在同一组,则染成另一种颜色。<染色过程只对对应为1-N的数染色>这样,所有数最终只被染成两种颜色,则两种颜色分别表示在A,B栈中,最后用这个数组模拟过程即可。注意:加栈元素时,栈中元素应是从栈底到栈顶递减的
<证明略,请见其他大牛博客,这里只谈做法>
【代码】:
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> #include<iostream> #include<stack> using namespace std; #define MAX 1001 int N,data[MAX],min1[MAX],fa[MAX*2],DATA=1,shouldout=1,color[2*MAX]; stack<int> S1; stack<int> S2; int get(int x){return x==fa[x] ? x : fa[x]=get(fa[x]);} bool can_a() { if(color[DATA]==1 && (S1.empty() || S1.top()>data[DATA])) return true; return false; } bool can_b() { if(!S1.empty() && S1.top()==shouldout) return true; return false; } bool can_c() { if(color[DATA]==2 && (S2.empty() || S2.top()>data[DATA])) return true; return false; } bool can_d() { if(!S2.empty() && S2.top()==shouldout) return true; return false; } void pre_getmin() { min1[N]=data[N]; for(int i=N-1;i>=1;i--) min1[i]=min(min1[i+1],data[i]); } void pre_getfa() { for(int i=1;i<=2*N;i++) fa[i]=i; for(int i=1;i<N;i++) for(int j=i+1;j<=N;j++) if(data[i]<data[j] && min1[j]<data[i]) { int fatherx=get(i),fathery=get(j+N); fa[fathery]=fatherx; fatherx=get(i+N),fathery=get(j); fa[fathery]=fatherx; } } bool check_wrong() { for(int i=1;i<=N;i++) if(get(i)==get(i+N)) { printf("0\n"); return false; } return true; } void pre_getcolor() { memset(color,0,sizeof(color)); for(int i=1;i<=N;i++) if(!color[i]) { color[i]=1; for(int j=1;j<=N;j++) if(fa[i]==fa[j]) color[j]=1; for(int j=N+1;j<=2*N;j++) if(fa[i]==fa[j]) color[j-N]=2; } } void work() { for(int i=1;i<=2*N;i++) { if(can_a()) { S1.push(data[DATA]); DATA++; printf("a "); } else if(can_b()) { S1.pop(); shouldout++; printf("b "); } else if(can_c()) { S2.push(data[DATA]); DATA++; printf("c "); } else if(can_d()) { S2.pop(); shouldout++; printf("d "); } } } int main() { freopen("twostack.in","r",stdin); freopen("twostack.out","w",stdout); scanf("%d",&N); for(int i=1;i<=N;i++) scanf("%d",&data[i]); pre_getmin(); pre_getfa(); if(!check_wrong()) return 0; pre_getcolor(); work(); return 0; }
【测评信息】:
转载注明出处:http://blog.csdn.net/u011400953