#include <bits/stdc++.h>
using namespace std;
const int O = 4e2 + 5;
const int mod = 998244353;
const int inf = 1e9;
int n, pre[3][O], f[3][O][O][O];
vector <int> pos[3];
string s;
void Maximize(int &x, int y){
if (x < y) x = y;
}
void Minimize(int &x, int y){
if (x > y) x = y;
}
void Add(int &x, int y){
x += y;
if (x >= mod) x -= mod;
if (x < 0) x += mod;
}
main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> s; s = " " + s;
for (int i = 1; i <= n; ++ i){
if (s[i] == 'R') pos[0].push_back(i);
if (s[i] == 'G') pos[1].push_back(i);
if (s[i] == 'Y') pos[2].push_back(i);
}
if (max({pos[0].size(), pos[1].size(), pos[2].size()}) > (n + 1) / 2) return cout << -1, 0;
for (int i = 0; i <= n; ++ i){
for (int j = 0; j <= n; ++ j){
for (int z = 0; z <= n; ++ z)
for (int l = 0; l < 3; ++ l) f[l][i][j][z] = inf;
}
}
for (int i = 0; i < 3; ++ i){
for (int j = 1; j <= n; ++ j)
pre[i][j] = upper_bound(pos[i].begin(), pos[i].end(), j) - pos[i].begin() + 1;
}
int Rsize = pos[0].size();
int Gsize = pos[1].size();
int Wsize = pos[2].size();
/// f[c][i][j][z] : - j is nums of green
/// - z is nums of white
///
for (int i = 0; i < 3; ++ i) f[i][0][0][0] = 0;
for (int i = 1; i <= n; ++ i){
for (int j = 0; j <= i && j <= (i + 1) / 2 && j <= Gsize; ++ j){
for (int z = 0; z <= i - j && z <= (i + 1) / 2 && z <= Wsize; ++ z){
int l = i - j - z;
if (l > (i + 1) / 2 || l > Rsize) continue;
int posR = pos[0][l - 1];
int posG = pos[1][j - 1];
int posW = pos[2][z - 1];
/// Red
if (j + z < i){
int x = pos[0][l - 1];
int G = pre[1][x];
int W = pre[2][x];
//if (i == 1) cout << "vcl luon " << x << " " << G << " " << W << endl;
int val = max(0, max(0, j - G + 1) + max(0, z - W + 1) + x - i);
Minimize(f[0][i][j][z], min(f[1][i - 1][j][z], f[2][i - 1][j][z]) + val);
}
/// Green
if (j){
int x = pos[1][j - 1];
int R = pre[0][x];
int W = pre[2][x];
int val = max(0, max(0, l - R + 1) + max(0, z - W + 1) + x - i);
Minimize(f[1][i][j][z], min(f[0][i - 1][j - 1][z], f[2][i - 1][j - 1][z]) + val);
}
/// White
if (z){
int x = pos[2][z - 1];
int R = pre[0][x];
int G = pre[1][x];
int val = max(0, max(0, l - R + 1) + max(0, j - G + 1) + x - i);
Minimize(f[2][i][j][z], min(f[0][i - 1][j][z - 1], f[1][i - 1][j][z - 1]) + val);
}
//cout << "debug " << i << " " << j << " " << z << " " << f[0][i][j][z] << " " << f[1][i][j][z] << " " << f[2][i][j][z] << endl;
}
}
}
int G = pos[1].size();
int W = pos[2].size();
cout << min({f[0][n][G][W], f[1][n][G][W], f[2][n][G][W]});
}
Compilation message
joi2019_ho_t3.cpp:27:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
27 | main(){
| ^~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:36:60: warning: comparison of integer expressions of different signedness: 'long unsigned int' and 'int' [-Wsign-compare]
36 | if (max({pos[0].size(), pos[1].size(), pos[2].size()}) > (n + 1) / 2) return cout << -1, 0;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:67:21: warning: unused variable 'posR' [-Wunused-variable]
67 | int posR = pos[0][l - 1];
| ^~~~
joi2019_ho_t3.cpp:68:21: warning: unused variable 'posG' [-Wunused-variable]
68 | int posG = pos[1][j - 1];
| ^~~~
joi2019_ho_t3.cpp:69:21: warning: unused variable 'posW' [-Wunused-variable]
69 | int posW = pos[2][z - 1];
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
5 ms |
25040 KB |
Output is correct |
5 |
Correct |
6 ms |
35164 KB |
Output is correct |
6 |
Correct |
6 ms |
35164 KB |
Output is correct |
7 |
Correct |
6 ms |
35164 KB |
Output is correct |
8 |
Correct |
6 ms |
35364 KB |
Output is correct |
9 |
Correct |
6 ms |
35164 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
6 ms |
35164 KB |
Output is correct |
12 |
Correct |
6 ms |
35164 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
6 ms |
33308 KB |
Output is correct |
15 |
Correct |
5 ms |
35164 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
5 ms |
25040 KB |
Output is correct |
5 |
Correct |
6 ms |
35164 KB |
Output is correct |
6 |
Correct |
6 ms |
35164 KB |
Output is correct |
7 |
Correct |
6 ms |
35164 KB |
Output is correct |
8 |
Correct |
6 ms |
35364 KB |
Output is correct |
9 |
Correct |
6 ms |
35164 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
6 ms |
35164 KB |
Output is correct |
12 |
Correct |
6 ms |
35164 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
6 ms |
33308 KB |
Output is correct |
15 |
Correct |
5 ms |
35164 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
19 ms |
121436 KB |
Output is correct |
19 |
Correct |
19 ms |
121436 KB |
Output is correct |
20 |
Correct |
19 ms |
121436 KB |
Output is correct |
21 |
Correct |
18 ms |
121436 KB |
Output is correct |
22 |
Correct |
18 ms |
121436 KB |
Output is correct |
23 |
Correct |
19 ms |
121428 KB |
Output is correct |
24 |
Correct |
19 ms |
121436 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
19 ms |
121436 KB |
Output is correct |
28 |
Correct |
19 ms |
121428 KB |
Output is correct |
29 |
Correct |
18 ms |
121436 KB |
Output is correct |
30 |
Correct |
20 ms |
121436 KB |
Output is correct |
31 |
Correct |
19 ms |
121428 KB |
Output is correct |
32 |
Correct |
19 ms |
121528 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
18 ms |
117340 KB |
Output is correct |
36 |
Correct |
18 ms |
119388 KB |
Output is correct |
37 |
Correct |
17 ms |
115292 KB |
Output is correct |
38 |
Correct |
18 ms |
121436 KB |
Output is correct |
39 |
Correct |
19 ms |
121436 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10700 KB |
Output is correct |
2 |
Correct |
349 ms |
778524 KB |
Output is correct |
3 |
Correct |
173 ms |
776148 KB |
Output is correct |
4 |
Correct |
172 ms |
778368 KB |
Output is correct |
5 |
Correct |
178 ms |
778356 KB |
Output is correct |
6 |
Correct |
174 ms |
778280 KB |
Output is correct |
7 |
Correct |
172 ms |
776272 KB |
Output is correct |
8 |
Correct |
177 ms |
776532 KB |
Output is correct |
9 |
Correct |
171 ms |
774228 KB |
Output is correct |
10 |
Correct |
172 ms |
778212 KB |
Output is correct |
11 |
Correct |
174 ms |
778176 KB |
Output is correct |
12 |
Correct |
69 ms |
402772 KB |
Output is correct |
13 |
Correct |
99 ms |
534100 KB |
Output is correct |
14 |
Correct |
127 ms |
642752 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
5 ms |
25040 KB |
Output is correct |
5 |
Correct |
6 ms |
35164 KB |
Output is correct |
6 |
Correct |
6 ms |
35164 KB |
Output is correct |
7 |
Correct |
6 ms |
35164 KB |
Output is correct |
8 |
Correct |
6 ms |
35364 KB |
Output is correct |
9 |
Correct |
6 ms |
35164 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
6 ms |
35164 KB |
Output is correct |
12 |
Correct |
6 ms |
35164 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
6 ms |
33308 KB |
Output is correct |
15 |
Correct |
5 ms |
35164 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
19 ms |
121436 KB |
Output is correct |
19 |
Correct |
19 ms |
121436 KB |
Output is correct |
20 |
Correct |
19 ms |
121436 KB |
Output is correct |
21 |
Correct |
18 ms |
121436 KB |
Output is correct |
22 |
Correct |
18 ms |
121436 KB |
Output is correct |
23 |
Correct |
19 ms |
121428 KB |
Output is correct |
24 |
Correct |
19 ms |
121436 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
19 ms |
121436 KB |
Output is correct |
28 |
Correct |
19 ms |
121428 KB |
Output is correct |
29 |
Correct |
18 ms |
121436 KB |
Output is correct |
30 |
Correct |
20 ms |
121436 KB |
Output is correct |
31 |
Correct |
19 ms |
121428 KB |
Output is correct |
32 |
Correct |
19 ms |
121528 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
18 ms |
117340 KB |
Output is correct |
36 |
Correct |
18 ms |
119388 KB |
Output is correct |
37 |
Correct |
17 ms |
115292 KB |
Output is correct |
38 |
Correct |
18 ms |
121436 KB |
Output is correct |
39 |
Correct |
19 ms |
121436 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
2 ms |
10700 KB |
Output is correct |
43 |
Correct |
349 ms |
778524 KB |
Output is correct |
44 |
Correct |
173 ms |
776148 KB |
Output is correct |
45 |
Correct |
172 ms |
778368 KB |
Output is correct |
46 |
Correct |
178 ms |
778356 KB |
Output is correct |
47 |
Correct |
174 ms |
778280 KB |
Output is correct |
48 |
Correct |
172 ms |
776272 KB |
Output is correct |
49 |
Correct |
177 ms |
776532 KB |
Output is correct |
50 |
Correct |
171 ms |
774228 KB |
Output is correct |
51 |
Correct |
172 ms |
778212 KB |
Output is correct |
52 |
Correct |
174 ms |
778176 KB |
Output is correct |
53 |
Correct |
69 ms |
402772 KB |
Output is correct |
54 |
Correct |
99 ms |
534100 KB |
Output is correct |
55 |
Correct |
127 ms |
642752 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
201 ms |
778324 KB |
Output is correct |
59 |
Correct |
201 ms |
778340 KB |
Output is correct |
60 |
Correct |
196 ms |
774200 KB |
Output is correct |
61 |
Correct |
203 ms |
776244 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
177 ms |
778308 KB |
Output is correct |
64 |
Correct |
184 ms |
778184 KB |
Output is correct |
65 |
Correct |
186 ms |
776276 KB |
Output is correct |
66 |
Correct |
198 ms |
771924 KB |
Output is correct |
67 |
Correct |
195 ms |
778324 KB |
Output is correct |
68 |
Correct |
197 ms |
776032 KB |
Output is correct |
69 |
Correct |
199 ms |
778324 KB |
Output is correct |
70 |
Correct |
199 ms |
778284 KB |
Output is correct |
71 |
Correct |
195 ms |
776276 KB |
Output is correct |
72 |
Correct |
1 ms |
348 KB |
Output is correct |
73 |
Correct |
1 ms |
348 KB |
Output is correct |