#include <bits/stdc++.h>
using namespace std;
const int N = 401;
int count_r[N], count_g[N], count_y[N];
int where_r[N], where_g[N], where_y[N];
int swaps[N][N][N][3];
void set_min(int &a, int b) {
a = min(a, b);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
string s;
cin >> n >> s;
for (int i = 0; i < n; ++i) {
count_r[i + 1] = count_r[i] + (s[i] == 'R');
count_g[i + 1] = count_g[i] + (s[i] == 'G');
count_y[i + 1] = count_y[i] + (s[i] == 'Y');
if (s[i] == 'R') {
where_r[count_r[i]] = i;
} else if (s[i] == 'G') {
where_g[count_g[i]] = i;
} else {
where_y[count_y[i]] = i;
}
}
for (int r = 0; r <= n; ++r) {
for (int g = 0; g <= n; ++g) {
for (int y = 0; y <= n; ++y) {
for (int i = 0; i < 3; ++i) {
if (r + g + y > 0) {
swaps[r][g][y][i] = INT_MAX;
}
}
}
}
}
for (int r = 0; r <= n; ++r) {
for (int g = 0; g <= n; ++g) {
for (int y = 0; y <= n; ++y) {
for (int i = 0; i < 3; ++i) {
if (swaps[r][g][y][i] < INT_MAX) {
if (i != 0 && r < count_r[n]) {
set_min(swaps[r + 1][g][y][0], swaps[r][g][y][i] +
max(0, count_g[where_r[r]] - g) +
max(0, count_y[where_r[r]] - y)
);
}
if (i != 1 && g < count_g[n]) {
set_min(swaps[r][g + 1][y][1], swaps[r][g][y][i] +
max(0, count_r[where_g[g]] - r) +
max(0, count_y[where_g[g]] - y)
);
}
if (i != 2 && y < count_y[n]) {
set_min(swaps[r][g][y + 1][2], swaps[r][g][y][i] +
max(0, count_r[where_y[y]] - r) +
max(0, count_g[where_y[y]] - g)
);
}
}
}
}
}
}
int ans = INT_MAX;
for (int i = 0; i < 3; ++i) {
set_min(ans, swaps[count_r[n]][count_g[n]][count_y[n]][i]);
}
cout << (ans < INT_MAX ? ans : -1) << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
844 KB |
Output is correct |
5 |
Correct |
1 ms |
1344 KB |
Output is correct |
6 |
Correct |
2 ms |
1356 KB |
Output is correct |
7 |
Correct |
1 ms |
1356 KB |
Output is correct |
8 |
Correct |
2 ms |
1368 KB |
Output is correct |
9 |
Correct |
1 ms |
1356 KB |
Output is correct |
10 |
Correct |
1 ms |
1360 KB |
Output is correct |
11 |
Correct |
1 ms |
1356 KB |
Output is correct |
12 |
Correct |
1 ms |
1356 KB |
Output is correct |
13 |
Correct |
1 ms |
1356 KB |
Output is correct |
14 |
Correct |
1 ms |
1228 KB |
Output is correct |
15 |
Correct |
1 ms |
1348 KB |
Output is correct |
16 |
Correct |
1 ms |
1356 KB |
Output is correct |
17 |
Correct |
1 ms |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
844 KB |
Output is correct |
5 |
Correct |
1 ms |
1344 KB |
Output is correct |
6 |
Correct |
2 ms |
1356 KB |
Output is correct |
7 |
Correct |
1 ms |
1356 KB |
Output is correct |
8 |
Correct |
2 ms |
1368 KB |
Output is correct |
9 |
Correct |
1 ms |
1356 KB |
Output is correct |
10 |
Correct |
1 ms |
1360 KB |
Output is correct |
11 |
Correct |
1 ms |
1356 KB |
Output is correct |
12 |
Correct |
1 ms |
1356 KB |
Output is correct |
13 |
Correct |
1 ms |
1356 KB |
Output is correct |
14 |
Correct |
1 ms |
1228 KB |
Output is correct |
15 |
Correct |
1 ms |
1348 KB |
Output is correct |
16 |
Correct |
1 ms |
1356 KB |
Output is correct |
17 |
Correct |
1 ms |
1228 KB |
Output is correct |
18 |
Correct |
12 ms |
17940 KB |
Output is correct |
19 |
Correct |
12 ms |
17996 KB |
Output is correct |
20 |
Correct |
12 ms |
17984 KB |
Output is correct |
21 |
Correct |
14 ms |
18044 KB |
Output is correct |
22 |
Correct |
11 ms |
17948 KB |
Output is correct |
23 |
Correct |
12 ms |
18024 KB |
Output is correct |
24 |
Correct |
13 ms |
17996 KB |
Output is correct |
25 |
Correct |
11 ms |
17996 KB |
Output is correct |
26 |
Correct |
12 ms |
18000 KB |
Output is correct |
27 |
Correct |
13 ms |
17944 KB |
Output is correct |
28 |
Correct |
12 ms |
18044 KB |
Output is correct |
29 |
Correct |
13 ms |
17984 KB |
Output is correct |
30 |
Correct |
12 ms |
17996 KB |
Output is correct |
31 |
Correct |
11 ms |
17996 KB |
Output is correct |
32 |
Correct |
11 ms |
18040 KB |
Output is correct |
33 |
Correct |
12 ms |
17356 KB |
Output is correct |
34 |
Correct |
11 ms |
17356 KB |
Output is correct |
35 |
Correct |
11 ms |
16852 KB |
Output is correct |
36 |
Correct |
11 ms |
17356 KB |
Output is correct |
37 |
Correct |
10 ms |
16200 KB |
Output is correct |
38 |
Correct |
12 ms |
18040 KB |
Output is correct |
39 |
Correct |
12 ms |
17964 KB |
Output is correct |
40 |
Correct |
12 ms |
17404 KB |
Output is correct |
41 |
Correct |
12 ms |
17964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
989 ms |
757412 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
844 KB |
Output is correct |
5 |
Correct |
1 ms |
1344 KB |
Output is correct |
6 |
Correct |
2 ms |
1356 KB |
Output is correct |
7 |
Correct |
1 ms |
1356 KB |
Output is correct |
8 |
Correct |
2 ms |
1368 KB |
Output is correct |
9 |
Correct |
1 ms |
1356 KB |
Output is correct |
10 |
Correct |
1 ms |
1360 KB |
Output is correct |
11 |
Correct |
1 ms |
1356 KB |
Output is correct |
12 |
Correct |
1 ms |
1356 KB |
Output is correct |
13 |
Correct |
1 ms |
1356 KB |
Output is correct |
14 |
Correct |
1 ms |
1228 KB |
Output is correct |
15 |
Correct |
1 ms |
1348 KB |
Output is correct |
16 |
Correct |
1 ms |
1356 KB |
Output is correct |
17 |
Correct |
1 ms |
1228 KB |
Output is correct |
18 |
Correct |
12 ms |
17940 KB |
Output is correct |
19 |
Correct |
12 ms |
17996 KB |
Output is correct |
20 |
Correct |
12 ms |
17984 KB |
Output is correct |
21 |
Correct |
14 ms |
18044 KB |
Output is correct |
22 |
Correct |
11 ms |
17948 KB |
Output is correct |
23 |
Correct |
12 ms |
18024 KB |
Output is correct |
24 |
Correct |
13 ms |
17996 KB |
Output is correct |
25 |
Correct |
11 ms |
17996 KB |
Output is correct |
26 |
Correct |
12 ms |
18000 KB |
Output is correct |
27 |
Correct |
13 ms |
17944 KB |
Output is correct |
28 |
Correct |
12 ms |
18044 KB |
Output is correct |
29 |
Correct |
13 ms |
17984 KB |
Output is correct |
30 |
Correct |
12 ms |
17996 KB |
Output is correct |
31 |
Correct |
11 ms |
17996 KB |
Output is correct |
32 |
Correct |
11 ms |
18040 KB |
Output is correct |
33 |
Correct |
12 ms |
17356 KB |
Output is correct |
34 |
Correct |
11 ms |
17356 KB |
Output is correct |
35 |
Correct |
11 ms |
16852 KB |
Output is correct |
36 |
Correct |
11 ms |
17356 KB |
Output is correct |
37 |
Correct |
10 ms |
16200 KB |
Output is correct |
38 |
Correct |
12 ms |
18040 KB |
Output is correct |
39 |
Correct |
12 ms |
17964 KB |
Output is correct |
40 |
Correct |
12 ms |
17404 KB |
Output is correct |
41 |
Correct |
12 ms |
17964 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Execution timed out |
989 ms |
757412 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |