#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
int n;
string s;
cin >> n >> s;
int R = count(s.begin(), s.end(), 'R');
int G = count(s.begin(), s.end(), 'G');
int Y = count(s.begin(), s.end(), 'Y');
vector<int> red(n + 1), green(n + 1), yellow(n + 1);
for (int i = 0; i < n; i++) {
red[i + 1] = red[i] + (int)(s[i] == 'R');
green[i + 1] = green[i] + (int)(s[i] == 'G');
yellow[i + 1] = yellow[i] + (int)(s[i] == 'Y');
}
vector dp(R + 1, vector(G + 1, vector(Y + 1, vector<int>(4, INF))));
dp[R][G][Y][3] = 0;
for (int i = n; i > 0; i--) {
for (int r = 0; r <= R; r++) {
for (int g = 0; g <= G && r + g <= i; g++) {
int y = i - r - g;
if (y > Y) {
continue;
}
for (int l = 0; l < 4; l++) {
int Rq = red[i - 1], Gq = green[i - 1], Yq = yellow[i - 1];
if (l != 0 && r > 0) {
dp[r - 1][g][y][0] = min(dp[r - 1][g][y][0], dp[r][g][y][l] + abs(Rq - (r - 1)) + abs(Gq - g) + abs(Yq - y));
}
if (l != 1 && g > 0) {
dp[r][g - 1][y][1] = min(dp[r][g - 1][y][1], dp[r][g][y][l] + abs(Rq - r) + abs(Gq - (g - 1)) + abs(Yq - y));
}
if (l != 2 && y > 0) {
dp[r][g][y - 1][2] = min(dp[r][g][y - 1][2], dp[r][g][y][l] + abs(Rq - r) + abs(Gq - g) + abs(Yq - (y - 1)));
}
}
}
}
}
int ans = min({dp[0][0][0][0], dp[0][0][0][1], dp[0][0][0][2]});
assert(ans % 2 == 0);
cout << (ans == INF ? -1 : ans / 2) << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
0 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 |
0 ms |
212 KB |
Output is correct |
11 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
0 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 |
0 ms |
212 KB |
Output is correct |
11 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
14 ms |
3800 KB |
Output is correct |
3 |
Correct |
14 ms |
3788 KB |
Output is correct |
4 |
Correct |
16 ms |
3792 KB |
Output is correct |
5 |
Correct |
15 ms |
3796 KB |
Output is correct |
6 |
Correct |
16 ms |
3800 KB |
Output is correct |
7 |
Correct |
15 ms |
3668 KB |
Output is correct |
8 |
Correct |
16 ms |
3668 KB |
Output is correct |
9 |
Correct |
15 ms |
3692 KB |
Output is correct |
10 |
Correct |
15 ms |
3804 KB |
Output is correct |
11 |
Correct |
15 ms |
3808 KB |
Output is correct |
12 |
Correct |
3 ms |
1236 KB |
Output is correct |
13 |
Correct |
6 ms |
1960 KB |
Output is correct |
14 |
Correct |
9 ms |
2688 KB |
Output is correct |
15 |
Correct |
16 ms |
3808 KB |
Output is correct |
16 |
Correct |
15 ms |
3804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
0 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 |
0 ms |
212 KB |
Output is correct |
11 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |