题目:https://www.luogu.org/problemnew/show/P1155
思路...
看博客:https://www.cnblogs.com/Narh/p/9213825.html
二分图什么的,字典序什么的,考场上怎么想出来...
代码如下:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int const maxn=1005; int n,a[maxn],f[maxn],col[maxn],head[maxn],ct,pos[maxn]; bool flag; struct N{ int to,next; N(int t=0,int n=0):to(t),next(n) {} }edge[maxn*maxn]; void add(int x,int y){edge[++ct]=N(y,head[x]); head[x]=ct;} bool init() { f[n]=n+1;//! for(int i=n-1;i;i--) { f[i]=min(a[i+1],f[i+1]); for(int j=i+1;j<=n;j++) if(a[j]>a[i]&&f[j]<a[i])add(i,j),add(j,i); } return 0; } void dfs(int x) { for(int i=head[x];i;i=edge[i].next) { int u=edge[i].to; if(col[u]==col[x]){flag=1; return;} else if(!col[u])col[u]=(col[x]^1),dfs(u); if(flag)return; } } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); init(); for(int i=1;i<=n;i++) if(!col[i]) { col[i]=2; dfs(i); if(flag){printf("0 "); return 0;} } int nw=1; for(int i=1;i<=n;i++) { pos[a[i]]=col[i]; if(col[i]==2)printf("a "); else printf("c "); while(pos[nw]) { if(pos[nw]==2)printf("b "); else printf("d "); nw++; } } return 0; }