#include "bits/stdc++.h"
using namespace std;
using ll = long long;
int main() {
cin.tie(0)->sync_with_stdio(0);
int n;
string s;
cin >> n >> s;
vector<int> pr(n + 1), pg(n + 1), py(n + 1);
vector<int> vr, vg, vy;
for (int i = 1; i <= n; i++) {
pr[i] = pr[i - 1];
pg[i] = pg[i - 1];
py[i] = py[i - 1];
if (s[i - 1] == 'R') {
++pr[i];
vr.push_back(i);
}
else if (s[i - 1] == 'G') {
++pg[i];
vg.push_back(i);
}
else if (s[i - 1] == 'Y') {
++py[i];
vy.push_back(i);
}
}
int nr = (int)vr.size();
int ng = (int)vg.size();
int ny = (int)vy.size();
vector dp(nr + 1, vector(ng + 1, vector(ny + 1, vector(3, 1000000000))));
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for (int i = 0; i <= nr; i++) {
for (int j = 0; j <= ng; j++) {
for (int k = 0; k <= ny; k++) {
if (i > 0) {
int pos = vr[i - 1];
dp[i][j][k][0] = min(dp[i][j][k][0], dp[i - 1][j][k][1] + abs(pg[pos] - j) + abs(py[pos] - k));
dp[i][j][k][0] = min(dp[i][j][k][0], dp[i - 1][j][k][2] + abs(pg[pos] - j) + abs(py[pos] - k));
}
if (j > 0) {
int pos = vg[j - 1];
dp[i][j][k][1] = min(dp[i][j][k][1], dp[i][j - 1][k][0] + abs(pr[pos] - i) + abs(py[pos] - k));
dp[i][j][k][1] = min(dp[i][j][k][1], dp[i][j - 1][k][2] + abs(pr[pos] - i) + abs(py[pos] - k));
}
if (k > 0) {
int pos = vy[k - 1];
dp[i][j][k][2] = min(dp[i][j][k][2], dp[i][j][k - 1][0] + abs(pr[pos] - i) + abs(pg[pos] - j));
dp[i][j][k][2] = min(dp[i][j][k][2], dp[i][j][k - 1][1] + abs(pr[pos] - i) + abs(pg[pos] - j));
}
// for (int l = 0; l < 3; l++) {
// cout << i << " " << j << " " << k << " " << l << " : " << dp[i][j][k][l] << endl;
// }
}
}
}
int ans = min(dp[nr][ng][ny][0], min(dp[nr][ng][ny][1], dp[nr][ng][ny][2]));
cout << ((ans >= 10000000) ? -1 : ans / 2) << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
324 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
828 KB |
Output is correct |
19 |
Correct |
2 ms |
828 KB |
Output is correct |
20 |
Correct |
1 ms |
852 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
852 KB |
Output is correct |
29 |
Correct |
1 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
832 KB |
Output is correct |
31 |
Correct |
1 ms |
852 KB |
Output is correct |
32 |
Correct |
1 ms |
852 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
724 KB |
Output is correct |
36 |
Correct |
1 ms |
832 KB |
Output is correct |
37 |
Correct |
1 ms |
724 KB |
Output is correct |
38 |
Correct |
1 ms |
724 KB |
Output is correct |
39 |
Correct |
2 ms |
852 KB |
Output is correct |
40 |
Correct |
1 ms |
320 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 |
5 ms |
3796 KB |
Output is correct |
3 |
Correct |
5 ms |
3796 KB |
Output is correct |
4 |
Correct |
5 ms |
3796 KB |
Output is correct |
5 |
Correct |
5 ms |
3796 KB |
Output is correct |
6 |
Correct |
5 ms |
3796 KB |
Output is correct |
7 |
Correct |
6 ms |
3796 KB |
Output is correct |
8 |
Correct |
5 ms |
3800 KB |
Output is correct |
9 |
Correct |
5 ms |
3796 KB |
Output is correct |
10 |
Correct |
5 ms |
3796 KB |
Output is correct |
11 |
Correct |
5 ms |
3796 KB |
Output is correct |
12 |
Correct |
2 ms |
1236 KB |
Output is correct |
13 |
Correct |
3 ms |
1980 KB |
Output is correct |
14 |
Correct |
4 ms |
2684 KB |
Output is correct |
15 |
Correct |
5 ms |
3796 KB |
Output is correct |
16 |
Correct |
6 ms |
3796 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
828 KB |
Output is correct |
19 |
Correct |
2 ms |
828 KB |
Output is correct |
20 |
Correct |
1 ms |
852 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
852 KB |
Output is correct |
29 |
Correct |
1 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
832 KB |
Output is correct |
31 |
Correct |
1 ms |
852 KB |
Output is correct |
32 |
Correct |
1 ms |
852 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
724 KB |
Output is correct |
36 |
Correct |
1 ms |
832 KB |
Output is correct |
37 |
Correct |
1 ms |
724 KB |
Output is correct |
38 |
Correct |
1 ms |
724 KB |
Output is correct |
39 |
Correct |
2 ms |
852 KB |
Output is correct |
40 |
Correct |
1 ms |
320 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
5 ms |
3796 KB |
Output is correct |
44 |
Correct |
5 ms |
3796 KB |
Output is correct |
45 |
Correct |
5 ms |
3796 KB |
Output is correct |
46 |
Correct |
5 ms |
3796 KB |
Output is correct |
47 |
Correct |
5 ms |
3796 KB |
Output is correct |
48 |
Correct |
6 ms |
3796 KB |
Output is correct |
49 |
Correct |
5 ms |
3800 KB |
Output is correct |
50 |
Correct |
5 ms |
3796 KB |
Output is correct |
51 |
Correct |
5 ms |
3796 KB |
Output is correct |
52 |
Correct |
5 ms |
3796 KB |
Output is correct |
53 |
Correct |
2 ms |
1236 KB |
Output is correct |
54 |
Correct |
3 ms |
1980 KB |
Output is correct |
55 |
Correct |
4 ms |
2684 KB |
Output is correct |
56 |
Correct |
5 ms |
3796 KB |
Output is correct |
57 |
Correct |
6 ms |
3796 KB |
Output is correct |
58 |
Correct |
175 ms |
133104 KB |
Output is correct |
59 |
Correct |
175 ms |
133280 KB |
Output is correct |
60 |
Correct |
173 ms |
132280 KB |
Output is correct |
61 |
Correct |
189 ms |
133780 KB |
Output is correct |
62 |
Correct |
16 ms |
12372 KB |
Output is correct |
63 |
Correct |
28 ms |
20776 KB |
Output is correct |
64 |
Correct |
94 ms |
66624 KB |
Output is correct |
65 |
Correct |
130 ms |
95564 KB |
Output is correct |
66 |
Correct |
176 ms |
131784 KB |
Output is correct |
67 |
Correct |
178 ms |
131716 KB |
Output is correct |
68 |
Correct |
175 ms |
133724 KB |
Output is correct |
69 |
Correct |
184 ms |
134816 KB |
Output is correct |
70 |
Correct |
178 ms |
134100 KB |
Output is correct |
71 |
Correct |
179 ms |
131016 KB |
Output is correct |
72 |
Correct |
47 ms |
34104 KB |
Output is correct |
73 |
Correct |
18 ms |
10596 KB |
Output is correct |