#include <bits/stdc++.h>
#ifdef DEBUG
#include "debug.hpp"
#else
#pragma GCC optimize("Ofast")
#define trace(...)
#include <bits/stdc++.h>
#define endl '\n'
#endif
using namespace std;
const string COL = "RGY";
int count_swaps(string s, string const& t) {
int n = s.length();
int ans = 0;
for (int i = 0; i < n; ++i) {
int j;
for (j = i; j < n; ++j) {
if (s[j] == t[i]) break;
}
if (j == n) return INT_MAX;
for (; j > i; --j) {
++ans;
swap(s[j], s[j - 1]);
}
}
return ans;
}
int subtask_1(int n, string s) {
int ans = INT_MAX;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 1 << (n - 1); ++j) {
string t;
int prev_c = i;
t.push_back(COL[prev_c]);
for (int k = 0; k < n - 1; ++k) {
int bit = (j >> k) & 1;
prev_c = (prev_c + bit + 1) % 3;
t.push_back(COL[prev_c]);
}
ans = min(count_swaps(s, t), ans);
}
}
if (ans == INT_MAX) ans = -1;
return ans;
}
int subtask_3(int n, string s) {
string t1, t2;
for (int i = 0; i < n; ++i) {
t1.push_back("RG"[i % 2]);
t2.push_back("GR"[i % 2]);
}
int ans = min(count_swaps(s, t1), count_swaps(s, t2));
if (ans == INT_MAX) ans = -1;
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
string s;
cin >> n >> s;
int ans;
if (n <= 15) {
ans = subtask_1(n, s);
} else {
ans = subtask_3(n, s);
}
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
14 ms |
376 KB |
Output is correct |
6 |
Correct |
18 ms |
416 KB |
Output is correct |
7 |
Correct |
17 ms |
376 KB |
Output is correct |
8 |
Correct |
17 ms |
376 KB |
Output is correct |
9 |
Correct |
14 ms |
376 KB |
Output is correct |
10 |
Correct |
8 ms |
376 KB |
Output is correct |
11 |
Correct |
17 ms |
376 KB |
Output is correct |
12 |
Correct |
20 ms |
376 KB |
Output is correct |
13 |
Correct |
13 ms |
376 KB |
Output is correct |
14 |
Correct |
10 ms |
376 KB |
Output is correct |
15 |
Correct |
20 ms |
376 KB |
Output is correct |
16 |
Correct |
11 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
14 ms |
376 KB |
Output is correct |
6 |
Correct |
18 ms |
416 KB |
Output is correct |
7 |
Correct |
17 ms |
376 KB |
Output is correct |
8 |
Correct |
17 ms |
376 KB |
Output is correct |
9 |
Correct |
14 ms |
376 KB |
Output is correct |
10 |
Correct |
8 ms |
376 KB |
Output is correct |
11 |
Correct |
17 ms |
376 KB |
Output is correct |
12 |
Correct |
20 ms |
376 KB |
Output is correct |
13 |
Correct |
13 ms |
376 KB |
Output is correct |
14 |
Correct |
10 ms |
376 KB |
Output is correct |
15 |
Correct |
20 ms |
376 KB |
Output is correct |
16 |
Correct |
11 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
552 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
7 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
14 ms |
376 KB |
Output is correct |
6 |
Correct |
18 ms |
416 KB |
Output is correct |
7 |
Correct |
17 ms |
376 KB |
Output is correct |
8 |
Correct |
17 ms |
376 KB |
Output is correct |
9 |
Correct |
14 ms |
376 KB |
Output is correct |
10 |
Correct |
8 ms |
376 KB |
Output is correct |
11 |
Correct |
17 ms |
376 KB |
Output is correct |
12 |
Correct |
20 ms |
376 KB |
Output is correct |
13 |
Correct |
13 ms |
376 KB |
Output is correct |
14 |
Correct |
10 ms |
376 KB |
Output is correct |
15 |
Correct |
20 ms |
376 KB |
Output is correct |
16 |
Correct |
11 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |