#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 <= count_r[n]; ++r) {
for (int g = 0; g <= count_g[n]; ++g) {
for (int y = 0; y <= count_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 <= count_r[n]; ++r) {
for (int g = 0; g <= count_g[n]; ++g) {
for (int y = 0; y <= count_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 |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
452 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
452 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
1 ms |
1868 KB |
Output is correct |
20 |
Correct |
2 ms |
2244 KB |
Output is correct |
21 |
Correct |
2 ms |
2380 KB |
Output is correct |
22 |
Correct |
1 ms |
1600 KB |
Output is correct |
23 |
Correct |
2 ms |
2124 KB |
Output is correct |
24 |
Correct |
1 ms |
1612 KB |
Output is correct |
25 |
Correct |
2 ms |
3916 KB |
Output is correct |
26 |
Correct |
2 ms |
4172 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
2 ms |
2252 KB |
Output is correct |
29 |
Correct |
2 ms |
2252 KB |
Output is correct |
30 |
Correct |
2 ms |
2256 KB |
Output is correct |
31 |
Correct |
1 ms |
1984 KB |
Output is correct |
32 |
Correct |
2 ms |
2380 KB |
Output is correct |
33 |
Correct |
2 ms |
3788 KB |
Output is correct |
34 |
Correct |
2 ms |
3532 KB |
Output is correct |
35 |
Correct |
2 ms |
2636 KB |
Output is correct |
36 |
Correct |
1 ms |
1868 KB |
Output is correct |
37 |
Correct |
1 ms |
1740 KB |
Output is correct |
38 |
Correct |
2 ms |
2124 KB |
Output is correct |
39 |
Correct |
2 ms |
2252 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
2124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
85 ms |
162864 KB |
Output is correct |
3 |
Correct |
78 ms |
162116 KB |
Output is correct |
4 |
Correct |
91 ms |
162884 KB |
Output is correct |
5 |
Correct |
77 ms |
162864 KB |
Output is correct |
6 |
Correct |
78 ms |
162932 KB |
Output is correct |
7 |
Correct |
80 ms |
162092 KB |
Output is correct |
8 |
Correct |
77 ms |
162056 KB |
Output is correct |
9 |
Correct |
76 ms |
161352 KB |
Output is correct |
10 |
Correct |
77 ms |
162892 KB |
Output is correct |
11 |
Correct |
79 ms |
162896 KB |
Output is correct |
12 |
Correct |
21 ms |
43980 KB |
Output is correct |
13 |
Correct |
40 ms |
77104 KB |
Output is correct |
14 |
Correct |
55 ms |
111336 KB |
Output is correct |
15 |
Correct |
78 ms |
162928 KB |
Output is correct |
16 |
Correct |
78 ms |
162884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
1 ms |
452 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
1 ms |
1868 KB |
Output is correct |
20 |
Correct |
2 ms |
2244 KB |
Output is correct |
21 |
Correct |
2 ms |
2380 KB |
Output is correct |
22 |
Correct |
1 ms |
1600 KB |
Output is correct |
23 |
Correct |
2 ms |
2124 KB |
Output is correct |
24 |
Correct |
1 ms |
1612 KB |
Output is correct |
25 |
Correct |
2 ms |
3916 KB |
Output is correct |
26 |
Correct |
2 ms |
4172 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
2 ms |
2252 KB |
Output is correct |
29 |
Correct |
2 ms |
2252 KB |
Output is correct |
30 |
Correct |
2 ms |
2256 KB |
Output is correct |
31 |
Correct |
1 ms |
1984 KB |
Output is correct |
32 |
Correct |
2 ms |
2380 KB |
Output is correct |
33 |
Correct |
2 ms |
3788 KB |
Output is correct |
34 |
Correct |
2 ms |
3532 KB |
Output is correct |
35 |
Correct |
2 ms |
2636 KB |
Output is correct |
36 |
Correct |
1 ms |
1868 KB |
Output is correct |
37 |
Correct |
1 ms |
1740 KB |
Output is correct |
38 |
Correct |
2 ms |
2124 KB |
Output is correct |
39 |
Correct |
2 ms |
2252 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
2124 KB |
Output is correct |
42 |
Correct |
1 ms |
320 KB |
Output is correct |
43 |
Correct |
85 ms |
162864 KB |
Output is correct |
44 |
Correct |
78 ms |
162116 KB |
Output is correct |
45 |
Correct |
91 ms |
162884 KB |
Output is correct |
46 |
Correct |
77 ms |
162864 KB |
Output is correct |
47 |
Correct |
78 ms |
162932 KB |
Output is correct |
48 |
Correct |
80 ms |
162092 KB |
Output is correct |
49 |
Correct |
77 ms |
162056 KB |
Output is correct |
50 |
Correct |
76 ms |
161352 KB |
Output is correct |
51 |
Correct |
77 ms |
162892 KB |
Output is correct |
52 |
Correct |
79 ms |
162896 KB |
Output is correct |
53 |
Correct |
21 ms |
43980 KB |
Output is correct |
54 |
Correct |
40 ms |
77104 KB |
Output is correct |
55 |
Correct |
55 ms |
111336 KB |
Output is correct |
56 |
Correct |
78 ms |
162928 KB |
Output is correct |
57 |
Correct |
78 ms |
162884 KB |
Output is correct |
58 |
Correct |
73 ms |
75384 KB |
Output is correct |
59 |
Correct |
87 ms |
95740 KB |
Output is correct |
60 |
Correct |
80 ms |
86988 KB |
Output is correct |
61 |
Correct |
81 ms |
84048 KB |
Output is correct |
62 |
Correct |
82 ms |
160580 KB |
Output is correct |
63 |
Correct |
85 ms |
160196 KB |
Output is correct |
64 |
Correct |
90 ms |
150084 KB |
Output is correct |
65 |
Correct |
92 ms |
138564 KB |
Output is correct |
66 |
Correct |
82 ms |
85240 KB |
Output is correct |
67 |
Correct |
76 ms |
82040 KB |
Output is correct |
68 |
Correct |
81 ms |
87852 KB |
Output is correct |
69 |
Correct |
79 ms |
84548 KB |
Output is correct |
70 |
Correct |
83 ms |
91544 KB |
Output is correct |
71 |
Correct |
79 ms |
83056 KB |
Output is correct |
72 |
Correct |
27 ms |
41176 KB |
Output is correct |
73 |
Correct |
3 ms |
2764 KB |
Output is correct |