被 Hack 力!
WA on Test #11, #13,求大佬看看哪错了。
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e3 + 5;
struct node{
int v, nxt;
}e[N * N * 2];
int n, a[N];
int stk1[N], stk2[N], tot1, tot2;
int rm[N], col[N];
int head[N], cnt;
int b[N];
void ad(int u, int v){
e[++cnt].v = v, e[cnt].nxt = head[u];
head[u] = cnt;
}
bool dfs(int u){
for(int i = head[u]; i; i = e[i].nxt){
int v = e[i].v;
if(col[v] == col[u]) return false;
if(col[v] != -1) continue;
col[v] = !col[u];
dfs(v);
}
return true;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n;
for(int i = 1;i <= n;i++){
cin>>a[i];
b[i] = a[i];
}
sort(b + 1, b + n + 1);
rm[n] = a[n];
for(int i = n-1;i;i--){
rm[i] = min(rm[i+1], a[i]);
}
for(int i = 1;i <= n;i++){
for(int j = i + 1;j + 1 <= n;j++){
if(a[i] < a[j] && a[i] > rm[j + 1]){
// cout<<i<<" "<<j<<"\n";
ad(i, j);
ad(j, i);
}
}
}
memset(col, -1, sizeof col);
bool f = 1;
for(int i = 1;i <= n;i++){
if(col[i] == -1){
col[i] = 1;
f = f && dfs(i);
}
}
if(!f){
cout<<"0\n";
return 0;
}
cout<<"a ";
stk1[++tot1] = 1;
int now = 1;
int scnt = 1;
while(tot1 || tot2){
now++;
while(tot1 >= 1 && a[stk1[tot1]] == b[scnt]){
scnt++;
cout<<"b ";
tot1--;
}
while(tot2 >= 1 && a[stk2[tot2]] == b[scnt]){
scnt++;
cout<<"d ";
tot2--;
}
if(now > n) continue;
if(tot1 == 0){
stk1[++tot1] = now;
cout<<"a ";
}
else if(col[stk1[tot1]] == col[now]){
stk1[++tot1] = now;
cout<<"a ";
}
else{
stk2[++tot2] = now;
cout<<"c ";
}
// for(int i = 1;i <= tot1;i++) cout<<stk1[i]<<" \n"[i == tot1];
// for(int i = 1;i <= tot2;i++) cout<<stk2[i]<<" \n"[i == tot2];
}
return 0;
}