#include <bits/stdc++.h>
using namespace std;
const int N = 401;
int prefSum[N][3];
int dp[N][N][N][3];
int main(){
int n;
cin >> n;
string s;
cin >> s;
vector<int> red;
vector<int> green;
vector<int> yellow;
for(int i = 0; i < n; i++){
if(s[i] == 'R'){
red.push_back(i + 1);
}
if(s[i] == 'G'){
green.push_back(i + 1);
}
if(s[i] == 'Y'){
yellow.push_back(i + 1);
}
prefSum[i + 1][0] = (int)red.size();
prefSum[i + 1][1] = (int)green.size();
prefSum[i + 1][2] = (int)yellow.size();
}
for(int i = 0; i <= (int)red.size(); i++){
for(int j = 0; j <= (int)green.size(); j++){
for(int k = 0; k <= (int)yellow.size(); k++){
if(i != 0 || j != 0 || k != 0){
for(int l = 0; l < 3; l++){
dp[i][j][k][l] = 1e9;
}
int sum = i + j + k;
if(i != 0){
dp[i][j][k][0] = min(dp[i - 1][j][k][1], dp[i - 1][j][k][2]) + ((red[i - 1] + max(0, j - prefSum[red[i - 1]][1]) + max(0, k - prefSum[red[i - 1]][2])) - sum);
}
if(j != 0){
dp[i][j][k][1] = min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]) + ((green[j - 1] + max(0, i - prefSum[green[j - 1]][0]) + max(0, k - prefSum[green[j - 1]][2])) - sum);
}
if(k != 0){
dp[i][j][k][2] = min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]) + ((yellow[k - 1] + max(0, i - prefSum[yellow[k - 1]][0]) + max(0, j - prefSum[yellow[k - 1]][1])) - sum);
}
}
}
}
}
int ans = *min_element(dp[red.size()][green.size()][yellow.size()], dp[red.size()][green.size()][yellow.size()] + 3);
if(ans >= 1e9){
cout << -1 << "\n";
}
else{
cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
436 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
436 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
2272 KB |
Output is correct |
19 |
Correct |
1 ms |
1848 KB |
Output is correct |
20 |
Correct |
2 ms |
2260 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1588 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4152 KB |
Output is correct |
27 |
Correct |
1 ms |
3000 KB |
Output is correct |
28 |
Correct |
1 ms |
2232 KB |
Output is correct |
29 |
Correct |
1 ms |
2232 KB |
Output is correct |
30 |
Correct |
1 ms |
2260 KB |
Output is correct |
31 |
Correct |
1 ms |
1972 KB |
Output is correct |
32 |
Correct |
2 ms |
2388 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
1 ms |
3540 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
36 |
Correct |
1 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1620 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
1 ms |
2260 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 |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
64 ms |
162892 KB |
Output is correct |
3 |
Correct |
52 ms |
162112 KB |
Output is correct |
4 |
Correct |
51 ms |
162912 KB |
Output is correct |
5 |
Correct |
48 ms |
162888 KB |
Output is correct |
6 |
Correct |
49 ms |
162868 KB |
Output is correct |
7 |
Correct |
48 ms |
162048 KB |
Output is correct |
8 |
Correct |
77 ms |
162116 KB |
Output is correct |
9 |
Correct |
66 ms |
161344 KB |
Output is correct |
10 |
Correct |
69 ms |
162856 KB |
Output is correct |
11 |
Correct |
64 ms |
162908 KB |
Output is correct |
12 |
Correct |
18 ms |
44036 KB |
Output is correct |
13 |
Correct |
30 ms |
77140 KB |
Output is correct |
14 |
Correct |
44 ms |
111328 KB |
Output is correct |
15 |
Correct |
61 ms |
162948 KB |
Output is correct |
16 |
Correct |
66 ms |
162940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
436 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
2272 KB |
Output is correct |
19 |
Correct |
1 ms |
1848 KB |
Output is correct |
20 |
Correct |
2 ms |
2260 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1588 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4152 KB |
Output is correct |
27 |
Correct |
1 ms |
3000 KB |
Output is correct |
28 |
Correct |
1 ms |
2232 KB |
Output is correct |
29 |
Correct |
1 ms |
2232 KB |
Output is correct |
30 |
Correct |
1 ms |
2260 KB |
Output is correct |
31 |
Correct |
1 ms |
1972 KB |
Output is correct |
32 |
Correct |
2 ms |
2388 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
1 ms |
3540 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
36 |
Correct |
1 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1620 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
1 ms |
2260 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
64 ms |
162892 KB |
Output is correct |
44 |
Correct |
52 ms |
162112 KB |
Output is correct |
45 |
Correct |
51 ms |
162912 KB |
Output is correct |
46 |
Correct |
48 ms |
162888 KB |
Output is correct |
47 |
Correct |
49 ms |
162868 KB |
Output is correct |
48 |
Correct |
48 ms |
162048 KB |
Output is correct |
49 |
Correct |
77 ms |
162116 KB |
Output is correct |
50 |
Correct |
66 ms |
161344 KB |
Output is correct |
51 |
Correct |
69 ms |
162856 KB |
Output is correct |
52 |
Correct |
64 ms |
162908 KB |
Output is correct |
53 |
Correct |
18 ms |
44036 KB |
Output is correct |
54 |
Correct |
30 ms |
77140 KB |
Output is correct |
55 |
Correct |
44 ms |
111328 KB |
Output is correct |
56 |
Correct |
61 ms |
162948 KB |
Output is correct |
57 |
Correct |
66 ms |
162940 KB |
Output is correct |
58 |
Correct |
41 ms |
75388 KB |
Output is correct |
59 |
Correct |
49 ms |
95636 KB |
Output is correct |
60 |
Correct |
42 ms |
86888 KB |
Output is correct |
61 |
Correct |
49 ms |
84008 KB |
Output is correct |
62 |
Correct |
49 ms |
160592 KB |
Output is correct |
63 |
Correct |
57 ms |
160180 KB |
Output is correct |
64 |
Correct |
50 ms |
150024 KB |
Output is correct |
65 |
Correct |
72 ms |
138472 KB |
Output is correct |
66 |
Correct |
55 ms |
85196 KB |
Output is correct |
67 |
Correct |
50 ms |
82064 KB |
Output is correct |
68 |
Correct |
56 ms |
87824 KB |
Output is correct |
69 |
Correct |
52 ms |
84500 KB |
Output is correct |
70 |
Correct |
56 ms |
91548 KB |
Output is correct |
71 |
Correct |
77 ms |
83096 KB |
Output is correct |
72 |
Correct |
20 ms |
41164 KB |
Output is correct |
73 |
Correct |
2 ms |
2772 KB |
Output is correct |