2017.2.18[codevs1170]NOIP2008提高组复赛T4双栈排序
体面不贴
这题一开始卡了我好久……策了好久贪心都判断不了无解情况……
直到看了题解才发现自己有多傻逼……
传送门:http://blog.csdn.net/kqzxcmh/article/details/9566813
题解写的很清楚这里就不赘述了。
两次AC,还行吧。
关键是我太蒟蒻……
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 #define N 1010 7 #define RG register 8 #define inf 0x3f3f3f3f 9 using namespace std; 10 char ans[N]; 11 bool mat[N][N]; 12 int n,top1,top2,top,topo,inn[N],col[N],dp[N],sta1[N],sta2[N]; 13 inline int Min(RG const int &a,RG const int &b){return a>b?b:a;} 14 inline int gi(){ 15 RG int x=0;RG char c=getchar(); 16 while(c<'0'||c>'9') c=getchar(); 17 while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar(); 18 return x; 19 } 20 inline bool dfs(RG int now){ 21 for (RG int i=1;i<=n;++i) 22 if(mat[now][i]){ 23 if(!col[i]){ 24 col[i]=3-col[now]; 25 if(!dfs(i)) return 0; 26 } 27 else if(col[i]==col[now]) 28 return 0; 29 } 30 return 1; 31 } 32 inline void out(){ 33 ++topo; 34 if(sta1[top1]==topo&&top1){ 35 ans[++top]='b'; 36 --top1; 37 } 38 else{ 39 ans[++top]='d'; 40 --top2; 41 } 42 } 43 inline void work(){ 44 n=gi();dp[n+1]=sta1[0]=sta2[0]=inf; 45 for (RG int i=1;i<=n;++i) inn[i]=gi(); 46 for (RG int i=n;i>=1;--i) dp[i]=Min(dp[i+1],inn[i]); 47 for (RG int i=1;i<n-1;++i) 48 for (RG int j=i+1;j<n;++j) 49 if(inn[j]>inn[i]&&dp[j+1]<inn[i]) 50 mat[i][j]=mat[j][i]=1; 51 for (RG int i=1;i<=n;++i) 52 if(!col[i]){ 53 col[i]=1; 54 if(!dfs(i)){ 55 printf("0\n"); 56 return; 57 } 58 } 59 for (RG int i=1;i<=n;++i){ 60 if(col[i]<2){ 61 while(inn[i]>sta1[top1]) out(); 62 ans[++top]='a'; 63 sta1[++top1]=inn[i]; 64 } 65 else{ 66 while(sta1[top1]==topo+1&&top1){ 67 ans[++top]='b'; 68 --top1; 69 ++topo; 70 } 71 while(inn[i]>sta2[top2]) out(); 72 ans[++top]='c'; 73 sta2[++top2]=inn[i]; 74 } 75 } 76 while(top1||top2) out(); 77 for (RG int i=1;i<=top;++i) printf("%c ",ans[i]); 78 putchar('\n'); 79 } 80 int main(){ 81 freopen("3114.in","r",stdin); 82 freopen("3114.out","w",stdout); 83 work(); 84 fclose(stdin); 85 fclose(stdout); 86 return 0; 87 }