#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
string s;
cin >> n >> s;
vector<int> pos[3];
for (int i = 0; i < n; i++) {
if (s[i] == 'R') {
pos[0].push_back(i);
} else if (s[i] == 'G') {
pos[1].push_back(i);
} else {
pos[2].push_back(i);
}
}
int R = pos[0].size(), G = pos[1].size(), Y = pos[2].size();
vector dp(R + 1, vector(G + 1, vector(Y + 1, vector<int>(4, 1e9))));
dp[0][0][0][3] = 0;
for (int i = 0; i < n; i++) {
for (int r = 0; r <= R && r <= i; 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++) {
if (l != 0 && r < R) {
int a = lower_bound(pos[1].begin(), pos[1].end(), pos[0][r]) - pos[1].begin();
int b = lower_bound(pos[2].begin(), pos[2].end(), pos[0][r]) - pos[2].begin();
dp[r + 1][g][y][0] = min(dp[r + 1][g][y][0], dp[r][g][y][l] + max(0, g - a) + max(0, y - b));
}
if (l != 1 && g < G) {
int a = lower_bound(pos[0].begin(), pos[0].end(), pos[1][g]) - pos[0].begin();
int b = lower_bound(pos[2].begin(), pos[2].end(), pos[1][g]) - pos[2].begin();
dp[r][g + 1][y][1] = min(dp[r][g + 1][y][1], dp[r][g][y][l] + max(0, r - a) + max(0, y - b));
}
if (l != 2 && y < Y) {
int a = lower_bound(pos[0].begin(), pos[0].end(), pos[2][y]) - pos[0].begin();
int b = lower_bound(pos[1].begin(), pos[1].end(), pos[2][y]) - pos[1].begin();
dp[r][g][y + 1][2] = min(dp[r][g][y + 1][2], dp[r][g][y][l] + max(0, r - a) + max(0, g - b));
}
}
}
}
}
int ans = min({dp[R][G][Y][0], dp[R][G][Y][1], dp[R][G][Y][2]});
cout << (ans == 1e9 ? -1 : ans) << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
3 ms |
840 KB |
Output is correct |
19 |
Correct |
3 ms |
852 KB |
Output is correct |
20 |
Correct |
3 ms |
836 KB |
Output is correct |
21 |
Correct |
3 ms |
852 KB |
Output is correct |
22 |
Correct |
3 ms |
724 KB |
Output is correct |
23 |
Correct |
3 ms |
852 KB |
Output is correct |
24 |
Correct |
3 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
448 KB |
Output is correct |
27 |
Correct |
3 ms |
724 KB |
Output is correct |
28 |
Correct |
2 ms |
852 KB |
Output is correct |
29 |
Correct |
2 ms |
852 KB |
Output is correct |
30 |
Correct |
2 ms |
852 KB |
Output is correct |
31 |
Correct |
2 ms |
724 KB |
Output is correct |
32 |
Correct |
3 ms |
852 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
2 ms |
468 KB |
Output is correct |
35 |
Correct |
3 ms |
724 KB |
Output is correct |
36 |
Correct |
2 ms |
724 KB |
Output is correct |
37 |
Correct |
2 ms |
724 KB |
Output is correct |
38 |
Correct |
3 ms |
724 KB |
Output is correct |
39 |
Correct |
3 ms |
852 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
30 ms |
3772 KB |
Output is correct |
3 |
Correct |
28 ms |
3796 KB |
Output is correct |
4 |
Correct |
31 ms |
3796 KB |
Output is correct |
5 |
Correct |
30 ms |
3796 KB |
Output is correct |
6 |
Correct |
30 ms |
3796 KB |
Output is correct |
7 |
Correct |
30 ms |
3776 KB |
Output is correct |
8 |
Correct |
31 ms |
3740 KB |
Output is correct |
9 |
Correct |
29 ms |
3688 KB |
Output is correct |
10 |
Correct |
29 ms |
3796 KB |
Output is correct |
11 |
Correct |
30 ms |
3824 KB |
Output is correct |
12 |
Correct |
6 ms |
1236 KB |
Output is correct |
13 |
Correct |
11 ms |
1876 KB |
Output is correct |
14 |
Correct |
18 ms |
2644 KB |
Output is correct |
15 |
Correct |
30 ms |
3796 KB |
Output is correct |
16 |
Correct |
34 ms |
3804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
3 ms |
840 KB |
Output is correct |
19 |
Correct |
3 ms |
852 KB |
Output is correct |
20 |
Correct |
3 ms |
836 KB |
Output is correct |
21 |
Correct |
3 ms |
852 KB |
Output is correct |
22 |
Correct |
3 ms |
724 KB |
Output is correct |
23 |
Correct |
3 ms |
852 KB |
Output is correct |
24 |
Correct |
3 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
448 KB |
Output is correct |
27 |
Correct |
3 ms |
724 KB |
Output is correct |
28 |
Correct |
2 ms |
852 KB |
Output is correct |
29 |
Correct |
2 ms |
852 KB |
Output is correct |
30 |
Correct |
2 ms |
852 KB |
Output is correct |
31 |
Correct |
2 ms |
724 KB |
Output is correct |
32 |
Correct |
3 ms |
852 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
2 ms |
468 KB |
Output is correct |
35 |
Correct |
3 ms |
724 KB |
Output is correct |
36 |
Correct |
2 ms |
724 KB |
Output is correct |
37 |
Correct |
2 ms |
724 KB |
Output is correct |
38 |
Correct |
3 ms |
724 KB |
Output is correct |
39 |
Correct |
3 ms |
852 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
30 ms |
3772 KB |
Output is correct |
44 |
Correct |
28 ms |
3796 KB |
Output is correct |
45 |
Correct |
31 ms |
3796 KB |
Output is correct |
46 |
Correct |
30 ms |
3796 KB |
Output is correct |
47 |
Correct |
30 ms |
3796 KB |
Output is correct |
48 |
Correct |
30 ms |
3776 KB |
Output is correct |
49 |
Correct |
31 ms |
3740 KB |
Output is correct |
50 |
Correct |
29 ms |
3688 KB |
Output is correct |
51 |
Correct |
29 ms |
3796 KB |
Output is correct |
52 |
Correct |
30 ms |
3824 KB |
Output is correct |
53 |
Correct |
6 ms |
1236 KB |
Output is correct |
54 |
Correct |
11 ms |
1876 KB |
Output is correct |
55 |
Correct |
18 ms |
2644 KB |
Output is correct |
56 |
Correct |
30 ms |
3796 KB |
Output is correct |
57 |
Correct |
34 ms |
3804 KB |
Output is correct |
58 |
Execution timed out |
1022 ms |
133328 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |