- 2008
T4
- @ 2025-9-12 21:09:40
#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))
{
cout << 0 << endl;
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();)
{
int j = i + 1;
while(j < res.size() && check(res[i], res[j])) j++;
sort(res.begin() + i, res.begin() + j);
i = j;
}
for(auto c : res)
cout << c << " ";
cout << endl;
return 0;
}
/*
如果只有一个栈:整个操作顺序是固定的
从前往后枚举每一个数,每次将当前的数压入栈中,如果后面的数均比栈顶元素大
则将栈顶弹出,否则栈顶不能被弹出
如果是两个栈:只需要考虑每个数入栈时,分配给哪个栈即可
性质:两个数i和j(i <= j),不能放入同一个栈中,
当且仅当存在k, k > j,且q[k] < q[i] < q[j]
二分图问题:
建图: 如果 i,j满足条件,则在i和j之间连一条边
最后判断此问题是否是二分图即可,用染色法。
*/
0 条评论
目前还没有评论...