1 solutions
-
1
#include<bits/stdc++.h> using namespace std; const int N=1010; int n; int a[N],f[N]; bool g[N][N]; int color[N]; bool dfs(int u,int c) { color[u]=c; for(int j=1;j<=n;j++) { if(!g[u][j]) continue; if(color[j]) { if(color[j]==c) return false; } else if(!dfs(j,3-c)) return false; } return true; } bool check(char a,char b) { if(a==b) return true; if(a>b) swap(a,b); return a=='b'&&b=='c'||a=='a'&&b=='d'; } int main() { cin>>n; for(int i=1;i<=n;i++) { cin>>a[i]; } f[n+1]=n+1; for(int i=n;i;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]) { g[i][j]=g[j][i]=true; } } } for(int i=1;i<=n;i++) { if(!color[i]&&!dfs(i,1)) { puts("0"); return 0; } } string res; stack<int> stk1,stk2; for(int i=1,cur=1;i<=n;i++) { if(color[i]==1) stk1.push(a[i]),res+='a'; else stk2.push(a[i]),res+='c'; while(stk1.size()&&stk1.top()==cur||stk2.size()&&stk2.top()==cur) { if(stk1.size()&&stk1.top()==cur) stk1.pop(),cur++,res+='b'; else stk2.pop(),cur++,res+='d'; } } for(int i=0;i<res.size();i++) { int j=i; while(j<res.size()&&check(res[i],res[j])) j++; j--; sort(res.begin()+i,res.begin()+j+1); i=j; } for(auto x:res) { cout<<x<<" "; } return 0; }
Information
- ID
- 2158
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 8
- Tags
- # Submissions
- 13
- Accepted
- 3
- Uploaded By