#include <bits/stdc++.h>
using namespace std;
#define inf 0x3F3F3F3F3F3F3F3F
const int MXN = 20 + 5;
const int mod = 1e9 + 7;
const int LOG = 20;
int n;
vector<array<int, 3>> res;
set<array<int, 3>> s;
queue<array<array<int, 3>, 2>> del[4];
int cnt[4];
int ok()
{
if (s.size() == 3)
{
return cnt[1] == 1 && cnt[2] == 1 && cnt[3] == 1;
}
if (max({cnt[1], cnt[2], cnt[3]}) == s.size())
{
return 0;
}
int mx = 1;
for (int i = 1; i <= 3; i++) if (cnt[i] > cnt[mx]) mx = i;
while (!del[mx].empty())
{
if (s.find(del[mx].front()[0]) == s.end() || s.find(del[mx].front()[1]) == s.end()) del[mx].pop();
else break;
}
assert(!del[mx].empty());
array<array<int, 3>, 2> d = del[mx].front();
del[mx].pop();
s.erase(d[0]);
cnt[d[0][2]]--;
s.erase(d[1]);
cnt[d[1][2]]--;
s.insert({d[0][0], d[1][1], 6 - d[0][2] - d[1][2]});
cnt[6 - d[0][2] - d[1][2]]++;
auto x = s.find({d[0][0], d[1][1], 6 - d[0][2] - d[1][2]});
auto z = (x == s.begin() ? prev(s.end()) : prev(x));
auto y = (next(x) == s.end() ? s.begin() : next(x));
array<int, 3> A = *z, B = *x, C = *y;
if (A[2] != B[2])
{
del[A[2]].push({A, B});
del[B[2]].push({A, B});
}
if (B[2] != C[2])
{
del[B[2]].push({B, C});
del[C[2]].push({B, C});
}
if (ok())
{
res.push_back({d[0][0], d[1][1], 6 - d[0][2] - d[1][2]});
return 1;
}
return 0;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
vector<array<int, 3>> v;
for (int i = 0; i < n; i++)
{
char ch;
cin >> ch;
s.insert({i + 1, (i + 1) % n + 1, ch - '0'});
cnt[ch - '0']++;
}
for (auto x = s.begin(); x != s.end(); x++)
{
auto y = (next(x) == s.end() ? s.begin() : next(x));
if ((*x)[2] != (*y)[2]) del[(*x)[2]].push({*x, *y});
}
if (cnt[3] % 2 != cnt[1] % 2 || cnt[3] % 2 != cnt[1] % 2 || cnt[1] % 2 != cnt[2] % 2)
{
cout << "NE\n";
return 0;
}
if (!ok()) cout << "NE\n";
else
{
cout << "DA\n";
for (array<int, 3> &x : res) cout << x[0] << ' ' << x[1] << ' ' << x[2] << '\n';
}
}
Compilation message
trobojnica.cpp: In function 'int ok()':
trobojnica.cpp:23:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::set<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | if (max({cnt[1], cnt[2], cnt[3]}) == s.size())
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
456 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
456 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
182 ms |
47008 KB |
Output is correct |
22 |
Correct |
173 ms |
48928 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
236 ms |
51752 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
118 ms |
26352 KB |
Output is correct |
28 |
Correct |
49 ms |
16208 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
38 ms |
12932 KB |
Output is correct |