#include <bits/stdc++.h>
using namespace std;
#define fr first
#define se second
const int INF = 0x3f3f3f3f;
string blz = "RGY";
vector<int> s[3];
int val[3];
int32_t main() {
//freopen("in", "r", stdin);
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++) {
char c;
cin >> c;
if (c == 'R') v[i] = 0;
else if (c == 'G') v[i] = 1;
else v[i] = 2;
}
// calc first batch
for (int i = 0; i < 3; i++) {
val[i] = INF;
vector<int> x = v;
for (int j = 0; j < n; j++) {
if (v[j] == i) {
x.erase(x.begin() + j);
x.insert(x.begin(), i);
val[i] = j;
s[i] = x;
break;
}
}
}
// calc all next ones
for (int k = 1; k < n; k++) {
vector<int> nVal(3, INF);
vector<vector<int>> nS(3);
for (int i = 0; i < 3; i++) {
if (val[i] == INF) continue;
for (int l = 0; l < 3; l++) {
if (l == i) continue;
vector<int> x = s[i];
for (int j = k; j < n; j++) {
if (x[j] == l) {
int np = (val[i] + (j - k));
if (np < nVal[l]) {
x.erase(x.begin() + j);
x.insert(x.begin() + k, l);
nS[l] = x;
nVal[l] = np;
}
break;
}
}
}
}
for (int i = 0; i < 3; i++) {
val[i] = nVal[i];
s[i] = nS[i];
}
}
// print ans
int ans = min(val[0], min(val[1], val[2]));
if (ans == INF) cout << "-1\n";
else cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
432 KB |
Output is correct |
3 |
Correct |
1 ms |
444 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
432 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |