#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
typedef long long ll;
int n, dp[401][401][401][3], sz[3], cnt[401][3];
vector<int> v[3];
string s, t = "RGY";
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> s;
for(int i = 0; i < n; i++)
for(int j = 0; j < 3; j++)
if(s[i] == t[j]) v[j].emplace_back(i);
for(int i = 0; i < 3; i++) sz[i] = v[i].size();
for(int i = 0; i < 3; i++) if(s[0] == t[i]) cnt[0][i] = 1;
for(int i = 1; i < n; i++){
for(int j = 0; j < 3; j++) cnt[i][j] = cnt[i - 1][j] + (s[i] == t[j]);
}
for(int i = 0; i <= sz[0]; i++)
for(int j = 0; j <= sz[1]; j++)
for(int k = 0; k <= sz[2]; k++)
for(int l = 0; l < 3; l++) dp[i][j][k][l] = 1e9;
for(int i = 0; i < 3; i++) dp[0][0][0][i] = 0;
for(int i = 0; i <= sz[0]; i++)
for(int j = 0; j <= sz[1]; j++)
for(int k = 0; k <= sz[2]; k++){
if(i + j + k){
if(i) dp[i][j][k][0] = min(dp[i - 1][j][k][1], dp[i - 1][j][k][2]) + max(j - cnt[v[0][i - 1]][1], 0) + max(k - cnt[v[0][i - 1]][2], 0);
if(j) dp[i][j][k][1] = min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]) + max(i - cnt[v[1][j - 1]][0], 0) + max(k - cnt[v[1][j - 1]][2], 0);
if(k) dp[i][j][k][2] = min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]) + max(i - cnt[v[2][k - 1]][0], 0) + max(j - cnt[v[2][k - 1]][1], 0);
}
}
int ans = 1e9;
for(int i = 0; i < 3; i++) ans = min(ans, dp[sz[0]][sz[1]][sz[2]][i]);
if(ans == 1e9) cout << -1;
else cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
1 ms |
488 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
1 ms |
488 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
1 ms |
1864 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1608 KB |
Output is correct |
23 |
Correct |
1 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1608 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3016 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2260 KB |
Output is correct |
30 |
Correct |
1 ms |
2260 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
1 ms |
2388 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
1 ms |
2632 KB |
Output is correct |
36 |
Correct |
1 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1740 KB |
Output is correct |
38 |
Correct |
1 ms |
2120 KB |
Output is correct |
39 |
Correct |
1 ms |
2248 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
54 ms |
162928 KB |
Output is correct |
3 |
Correct |
52 ms |
162128 KB |
Output is correct |
4 |
Correct |
51 ms |
162888 KB |
Output is correct |
5 |
Correct |
59 ms |
162904 KB |
Output is correct |
6 |
Correct |
52 ms |
162892 KB |
Output is correct |
7 |
Correct |
52 ms |
162124 KB |
Output is correct |
8 |
Correct |
54 ms |
162124 KB |
Output is correct |
9 |
Correct |
53 ms |
161244 KB |
Output is correct |
10 |
Correct |
54 ms |
162944 KB |
Output is correct |
11 |
Correct |
54 ms |
162956 KB |
Output is correct |
12 |
Correct |
19 ms |
44048 KB |
Output is correct |
13 |
Correct |
28 ms |
77160 KB |
Output is correct |
14 |
Correct |
39 ms |
111308 KB |
Output is correct |
15 |
Correct |
54 ms |
162948 KB |
Output is correct |
16 |
Correct |
54 ms |
162840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
1 ms |
488 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
1 ms |
1864 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1608 KB |
Output is correct |
23 |
Correct |
1 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1608 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3016 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2260 KB |
Output is correct |
30 |
Correct |
1 ms |
2260 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
1 ms |
2388 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
1 ms |
2632 KB |
Output is correct |
36 |
Correct |
1 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1740 KB |
Output is correct |
38 |
Correct |
1 ms |
2120 KB |
Output is correct |
39 |
Correct |
1 ms |
2248 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
54 ms |
162928 KB |
Output is correct |
44 |
Correct |
52 ms |
162128 KB |
Output is correct |
45 |
Correct |
51 ms |
162888 KB |
Output is correct |
46 |
Correct |
59 ms |
162904 KB |
Output is correct |
47 |
Correct |
52 ms |
162892 KB |
Output is correct |
48 |
Correct |
52 ms |
162124 KB |
Output is correct |
49 |
Correct |
54 ms |
162124 KB |
Output is correct |
50 |
Correct |
53 ms |
161244 KB |
Output is correct |
51 |
Correct |
54 ms |
162944 KB |
Output is correct |
52 |
Correct |
54 ms |
162956 KB |
Output is correct |
53 |
Correct |
19 ms |
44048 KB |
Output is correct |
54 |
Correct |
28 ms |
77160 KB |
Output is correct |
55 |
Correct |
39 ms |
111308 KB |
Output is correct |
56 |
Correct |
54 ms |
162948 KB |
Output is correct |
57 |
Correct |
54 ms |
162840 KB |
Output is correct |
58 |
Correct |
48 ms |
75404 KB |
Output is correct |
59 |
Correct |
59 ms |
95708 KB |
Output is correct |
60 |
Correct |
52 ms |
86968 KB |
Output is correct |
61 |
Correct |
50 ms |
83972 KB |
Output is correct |
62 |
Correct |
57 ms |
160572 KB |
Output is correct |
63 |
Correct |
60 ms |
160148 KB |
Output is correct |
64 |
Correct |
63 ms |
150112 KB |
Output is correct |
65 |
Correct |
61 ms |
138444 KB |
Output is correct |
66 |
Correct |
53 ms |
85184 KB |
Output is correct |
67 |
Correct |
48 ms |
81956 KB |
Output is correct |
68 |
Correct |
49 ms |
87740 KB |
Output is correct |
69 |
Correct |
49 ms |
84584 KB |
Output is correct |
70 |
Correct |
54 ms |
91596 KB |
Output is correct |
71 |
Correct |
49 ms |
83156 KB |
Output is correct |
72 |
Correct |
23 ms |
41172 KB |
Output is correct |
73 |
Correct |
2 ms |
2772 KB |
Output is correct |