#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 const& s, string const& t) {
int n = s.length();
vector<stack<int>> st(3);
int diff = 0;
for (int i = n - 1; i >= 0; --i) {
st[COL.find(t[i])].push(i);
}
for (int i = 0; i < n; ++i) {
int c = COL.find(s[i]);
if (st[c].empty()) return INT_MAX;
diff += abs(st[c].top() - i);
st[c].pop();
}
assert(diff % 2 == 0);
for (int i = 0; i < 3; ++i) {
assert(st[i].empty());
}
return diff / 2;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
string s;
cin >> n >> 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;
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
51 ms |
376 KB |
Output is correct |
6 |
Correct |
51 ms |
376 KB |
Output is correct |
7 |
Correct |
49 ms |
376 KB |
Output is correct |
8 |
Correct |
50 ms |
376 KB |
Output is correct |
9 |
Correct |
47 ms |
424 KB |
Output is correct |
10 |
Correct |
49 ms |
380 KB |
Output is correct |
11 |
Incorrect |
50 ms |
380 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
51 ms |
376 KB |
Output is correct |
6 |
Correct |
51 ms |
376 KB |
Output is correct |
7 |
Correct |
49 ms |
376 KB |
Output is correct |
8 |
Correct |
50 ms |
376 KB |
Output is correct |
9 |
Correct |
47 ms |
424 KB |
Output is correct |
10 |
Correct |
49 ms |
380 KB |
Output is correct |
11 |
Incorrect |
50 ms |
380 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Execution timed out |
1058 ms |
376 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
51 ms |
376 KB |
Output is correct |
6 |
Correct |
51 ms |
376 KB |
Output is correct |
7 |
Correct |
49 ms |
376 KB |
Output is correct |
8 |
Correct |
50 ms |
376 KB |
Output is correct |
9 |
Correct |
47 ms |
424 KB |
Output is correct |
10 |
Correct |
49 ms |
380 KB |
Output is correct |
11 |
Incorrect |
50 ms |
380 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |