#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int SIZE = 2e5 + 5;
int n;
string s;
set<tuple<int, int, int>> st;
set<pair<int, int>> p[4];
vector<tuple<int, int, int>> ans;
bool in(int i) {
auto it = st.lower_bound({i, 0, 0});
return it != st.end() && get<0>(*it) == i;
}
int color(int i) {
auto it = st.lower_bound({i, 0, 0});
return get<2>(*it);
}
void connect(int l, int r) {
auto lit = st.lower_bound({l, 0, 0});
auto [l1, r1, c1] = *lit;
st.erase(lit);
auto rit = st.lower_bound({r, 0, 0});
auto [l2, r2, c2] = *rit;
st.erase(rit);
ans.pb(l1, r2, 6 - c1 - c2);
st.emplace(l1, r2, 6 - c1 - c2);
auto it = st.lower_bound({l1, 0, 0});
if (it != st.begin()) lit = prev(it);
else lit = --st.end();
if (it != --st.end()) rit = next(it);
else rit = st.begin();
tie(l1, r1, c1) = *lit;
tie(l2, r2, c2) = *it;
if (c1 != c2) p[6 - c1 - c2].emplace(l1, r1);
tie(l1, r1, c1) = *it;
tie(l2, r2, c2) = *rit;
if (c1 != c2) p[6 - c1 - c2].emplace(l1, r1);
}
void solve() {
cin >> n >> s;
s = " " + s;
int cnt[4] = {};
FOR (i, 1, n) cnt[s[i] - '0']++;
FOR (i, 1, 3) {
int x = n - 3 - cnt[i];
if (x % 2 == 0) {
cout << "NE\n";
return;
}
x = (x + 1) / 2;
if (x < 0) {
cout << "NE\n";
return;
}
cnt[i] = x;
}
FOR (i, 1, n) {
st.emplace(i, i % n + 1, s[i] - '0');
if (s[i] != s[i % n + 1]) p[6 - (s[i] - '0') - (s[i % n + 1] - '0')].emplace(i, i % n + 1);
}
while (st.size() > 3) {
bool ok = 0;
while (ok == 0) {
ok = 1;
int l = 0, r = 0, ii = 0;
FOR (i, 1, 3) if (cnt[i] && p[i].size()) {
tie(l, r) = *p[i].begin();
p[i].erase(p[i].begin());
ii = i;
break;
}
if (!in(l) || !in(r)) {
ok = 0;
continue;
}
int cl = color(l), cr = color(r);
if (cl == cr || 6 - cl - cr != ii) {
ok = 0;
continue;
}
cnt[ii]--;
connect(l, r);
}
}
cout << "DA\n";
for (auto [a, b, c] : ans) cout << a << ' ' << b << ' ' << c << '\n';
}
int main() {
Waimai;
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
180 ms |
18572 KB |
Output is correct |
22 |
Correct |
212 ms |
15332 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
244 ms |
24896 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
116 ms |
11204 KB |
Output is correct |
28 |
Correct |
2 ms |
992 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
2 ms |
1000 KB |
Output is correct |