#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e9;
const int N = 405;
char a;
int n, r, g, y, rp[N], gp[N], yp[N], rpref[N], gpref[N], ypref[N];
vector<vector<vector<vector<int>>>> dp;
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i){
cin >> a;
rpref[i] += rpref[i - 1];
gpref[i] += gpref[i - 1];
ypref[i] += ypref[i - 1];
if(a == 'R') {rp[++r] = i; rpref[i]++;}
if(a == 'G') {gp[++g] = i; gpref[i]++;}
if(a == 'Y') {yp[++y] = i; ypref[i]++;}
}
if(max({r, g, y}) > (n + 1) / 2){
cout << -1;
return 0;
}
// cout << r << ' ' << g << ' ' << y << '\n';
// int dp[5][r + 5][g + 5][y + 5];
//
// cout << "OK\n";
// cout << sizeof(dp);
// for(int i = 0; i <= 2; ++i){
// for(int j = 0; j <= r; ++j){
// for(int k = 0; k <= g; ++k){
// for(int l = 0; l <= y; ++l){
// dp[i][j][k][l] = inf;
// }
// }
// }
// }
dp.resize(3);
for(auto& i : dp){
i.resize(r + 1);
for(auto& j : i){
j.resize(g + 1);
for(auto& k : j){
k.resize(y + 1);
for(int& x : k){
x = inf;
}
}
}
}
// cout << "OK\n";
// memset(dp, sizeof(dp), inf);
// cout << "OK\n";
dp[0][0][0][0] = 0;
dp[1][0][0][0] = 0;
dp[2][0][0][0] = 0;
int x;
for(int i = 0; i <= r; ++i)
for(int j = 0; j <= g; ++j)
for(int k = 0; k <= y; ++k){
if(i != 0){
x = max(gpref[rp[i]] - j, 0ll) + max(ypref[rp[i]] - k, 0ll);
dp[0][i][j][k] = min({dp[0][i][j][k], dp[1][i - 1][j][k] + x, dp[2][i - 1][j][k] + x});
}
if(j != 0){
x = max(rpref[gp[j]] - i, 0ll) + max(ypref[gp[j]] - k, 0ll);
dp[1][i][j][k] = min({dp[1][i][j][k], dp[0][i][j - 1][k] + x, dp[2][i][j - 1][k] + x});
}
if(k != 0){
x = max(rpref[yp[k]] - i, 0ll) + max(gpref[yp[k]] - j, 0ll);
dp[2][i][j][k] = min({dp[2][i][j][k], dp[0][i][j][k - 1] + x, dp[1][i][j][k - 1] + x});
}
}
cout << min(dp[0][r][g][y], min(dp[1][r][g][y], dp[2][r][g][y]));
return 0;
}
Compilation message
joi2019_ho_t3.cpp:13:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
13 | main(){
| ^~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:77:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
77 | for(int i = 0; i <= r; ++i)
| ^~~
joi2019_ho_t3.cpp:94:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
94 | cout << min(dp[0][r][g][y], min(dp[1][r][g][y], dp[2][r][g][y]));
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
0 ms |
316 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
0 ms |
316 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
312 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
568 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
572 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
0 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
588 KB |
Output is correct |
28 |
Correct |
1 ms |
588 KB |
Output is correct |
29 |
Correct |
1 ms |
588 KB |
Output is correct |
30 |
Correct |
1 ms |
568 KB |
Output is correct |
31 |
Correct |
1 ms |
568 KB |
Output is correct |
32 |
Correct |
1 ms |
588 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
572 KB |
Output is correct |
36 |
Correct |
1 ms |
460 KB |
Output is correct |
37 |
Correct |
1 ms |
460 KB |
Output is correct |
38 |
Correct |
1 ms |
460 KB |
Output is correct |
39 |
Correct |
1 ms |
588 KB |
Output is correct |
40 |
Correct |
0 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
10 ms |
6988 KB |
Output is correct |
3 |
Correct |
8 ms |
6828 KB |
Output is correct |
4 |
Correct |
8 ms |
6988 KB |
Output is correct |
5 |
Correct |
8 ms |
6988 KB |
Output is correct |
6 |
Correct |
8 ms |
6988 KB |
Output is correct |
7 |
Correct |
8 ms |
6860 KB |
Output is correct |
8 |
Correct |
10 ms |
6860 KB |
Output is correct |
9 |
Correct |
8 ms |
6860 KB |
Output is correct |
10 |
Correct |
8 ms |
6976 KB |
Output is correct |
11 |
Correct |
8 ms |
6988 KB |
Output is correct |
12 |
Correct |
2 ms |
1996 KB |
Output is correct |
13 |
Correct |
4 ms |
3388 KB |
Output is correct |
14 |
Correct |
7 ms |
4788 KB |
Output is correct |
15 |
Correct |
1 ms |
292 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
0 ms |
316 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
312 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
568 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
572 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
0 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
588 KB |
Output is correct |
28 |
Correct |
1 ms |
588 KB |
Output is correct |
29 |
Correct |
1 ms |
588 KB |
Output is correct |
30 |
Correct |
1 ms |
568 KB |
Output is correct |
31 |
Correct |
1 ms |
568 KB |
Output is correct |
32 |
Correct |
1 ms |
588 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
572 KB |
Output is correct |
36 |
Correct |
1 ms |
460 KB |
Output is correct |
37 |
Correct |
1 ms |
460 KB |
Output is correct |
38 |
Correct |
1 ms |
460 KB |
Output is correct |
39 |
Correct |
1 ms |
588 KB |
Output is correct |
40 |
Correct |
0 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
10 ms |
6988 KB |
Output is correct |
44 |
Correct |
8 ms |
6828 KB |
Output is correct |
45 |
Correct |
8 ms |
6988 KB |
Output is correct |
46 |
Correct |
8 ms |
6988 KB |
Output is correct |
47 |
Correct |
8 ms |
6988 KB |
Output is correct |
48 |
Correct |
8 ms |
6860 KB |
Output is correct |
49 |
Correct |
10 ms |
6860 KB |
Output is correct |
50 |
Correct |
8 ms |
6860 KB |
Output is correct |
51 |
Correct |
8 ms |
6976 KB |
Output is correct |
52 |
Correct |
8 ms |
6988 KB |
Output is correct |
53 |
Correct |
2 ms |
1996 KB |
Output is correct |
54 |
Correct |
4 ms |
3388 KB |
Output is correct |
55 |
Correct |
7 ms |
4788 KB |
Output is correct |
56 |
Correct |
1 ms |
292 KB |
Output is correct |
57 |
Correct |
0 ms |
332 KB |
Output is correct |
58 |
Correct |
69 ms |
58036 KB |
Output is correct |
59 |
Correct |
74 ms |
58464 KB |
Output is correct |
60 |
Correct |
75 ms |
58360 KB |
Output is correct |
61 |
Correct |
85 ms |
58820 KB |
Output is correct |
62 |
Correct |
1 ms |
204 KB |
Output is correct |
63 |
Correct |
17 ms |
12096 KB |
Output is correct |
64 |
Correct |
40 ms |
31272 KB |
Output is correct |
65 |
Correct |
56 ms |
43824 KB |
Output is correct |
66 |
Correct |
71 ms |
58068 KB |
Output is correct |
67 |
Correct |
85 ms |
57520 KB |
Output is correct |
68 |
Correct |
73 ms |
58948 KB |
Output is correct |
69 |
Correct |
82 ms |
59316 KB |
Output is correct |
70 |
Correct |
79 ms |
58752 KB |
Output is correct |
71 |
Correct |
72 ms |
57284 KB |
Output is correct |
72 |
Correct |
1 ms |
332 KB |
Output is correct |
73 |
Correct |
1 ms |
332 KB |
Output is correct |