-
Notifications
You must be signed in to change notification settings - Fork 0
/
P_1155_NOIP_2008_提高组_双栈排序.cpp
87 lines (82 loc) · 2.31 KB
/
P_1155_NOIP_2008_提高组_双栈排序.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
void solve() {
int n; cin >> n;
vector<int> w(n + 1);
for (int i = 1; i <= n; i ++ )
cin >> w[i];
vector<int> mn(n + 2);
mn[n + 1] = n + 1;
for (int i = n; i; i -- )
mn[i] = min(mn[i + 1], w[i]);
vector e(n + 1, vector<int>());
for (int i = 1; i <= n; i ++ )
for (int j = i + 1; j <= n; j ++ )
if (w[i] < w[j] && mn[j + 1] < w[i])
e[i].push_back(j), e[j].push_back(i);
vector<int> col(n + 1);
function<void()> fun = [&] () {
for (int i = 1; i <= n; i ++ ) {
if (!col[i]) {
queue<int> q;
col[i] = 1;
q.push(i);
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto v : e[u]) {
if (col[v]) {
if (col[v] == col[u]) {
cout << "0\n";
exit(0);
}
continue;
}
col[v] = 3 ^ col[u];
q.push(v);
}
}
}
}
};
fun();
int cur = 0;
vector<stack<int>> stk(3);
function<bool(int)> check = [&] (int id) {
if (stk[id].empty() || stk[id].top() != cur + 1) return false;
return true;
};
function<void(int)> p = [&] (int id) {
cur ++;
stk[id].pop();
cout << (id == 1 ? "b " : "d ");
};
for (int i = 1; i <= n; i ++ ) {
int id = col[i];
cerr << col[i] << " \n"[i == n];
if (id == 2)
while (check(1)) p(1);
while (!stk[id].empty() && stk[id].top() < w[i]) {
if (!check(id)) p(3 ^ id);
else p(id);
}
if (id == 2)
while (check(1)) p(1);
stk[id].push(w[i]);
cout << (id == 1 ? "a " : "c ");
}
while (!stk[1].empty())
if (!check(1)) p(2);
else p(1);
while (!stk[2].empty())
p(2);
cout << "\n";
}
int main (void) {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int T = 1;
while (T -- ) solve();
return 0;
}