#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
#define MAXN 405
int n;
int a[MAXN];
vector<int> pos[3];
int dp[MAXN][MAXN][MAXN][3];
int o = 1e8;
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
char ai;
for (int i=1; i<=n; i++) {
cin >> ai;
if (ai == 'R') a[i] = 0;
if (ai == 'G') a[i] = 1;
if (ai == 'Y') a[i] = 2;
pos[a[i]].push_back(i);
}
for (int i=1; i<=n; i++) {
for (int j=0; j<=i; j++) {
for (int k=0; k<=i; k++) {
dp[i][j][k][0] = dp[i][j][k][1] = dp[i][j][k][2] = 1e8;
}
}
}
for (int i=1; i<=n; i++) {
for (int j=0; j<=i/2+1; j++) {
for (int k=0; k<=i/2+1; k++) {
int l = i-j-k;
if (j+k > i || j > pos[0].size() || k > pos[1].size() || l > pos[2].size()) continue;
int score, cur;
if (j > 0) {
score = min(dp[i-1][j-1][k][1], dp[i-1][j-1][k][2]);
cur = pos[0][j-1];
cur += max(0l, k-(lower_bound(pos[1].begin(), pos[1].end(), pos[0][j-1])-pos[1].begin()));
cur += max(0l, l-(lower_bound(pos[2].begin(), pos[2].end(), pos[0][j-1])-pos[2].begin()));
score += abs(i-cur);
dp[i][j][k][0] = min(dp[i][j][k][0], score);
}
if (k > 0) {
score = min(dp[i-1][j][k-1][0], dp[i-1][j][k-1][2]);
cur = pos[1][k-1];
cur += max(0l, j-(lower_bound(pos[0].begin(), pos[0].end(), pos[1][k-1])-pos[0].begin()));
cur += max(0l, l-(lower_bound(pos[2].begin(), pos[2].end(), pos[1][k-1])-pos[2].begin()));
score += abs(i-cur);
dp[i][j][k][1] = min(dp[i][j][k][1], score);
}
if (l > 0) {
score = min(dp[i-1][j][k][0], dp[i-1][j][k][1]);
cur = pos[2][l-1];
cur += max(0l, j-(lower_bound(pos[0].begin(), pos[0].end(), pos[2][l-1])-pos[0].begin()));
cur += max(0l, k-(lower_bound(pos[1].begin(), pos[1].end(), pos[2][l-1])-pos[1].begin()));
score += abs(i-cur);
dp[i][j][k][2] = min(dp[i][j][k][2], score);
}
}
}
}
for (int j=0; j<=n; j++) {
for (int k=0; k<=n; k++) {
for (int e=0; e<3; e++) {
o = min(o, dp[n][j][k][e]);
}
}
}
if (o == 1e8) {
cout << -1 << endl;
} else {
cout << o << endl;
}
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:35:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | if (j+k > i || j > pos[0].size() || k > pos[1].size() || l > pos[2].size()) continue;
| ~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:35:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | if (j+k > i || j > pos[0].size() || k > pos[1].size() || l > pos[2].size()) continue;
| ~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:35:64: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | if (j+k > i || j > pos[0].size() || k > pos[1].size() || l > pos[2].size()) continue;
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
1 ms |
896 KB |
Output is correct |
6 |
Correct |
1 ms |
896 KB |
Output is correct |
7 |
Correct |
1 ms |
896 KB |
Output is correct |
8 |
Correct |
1 ms |
896 KB |
Output is correct |
9 |
Correct |
1 ms |
896 KB |
Output is correct |
10 |
Correct |
1 ms |
1024 KB |
Output is correct |
11 |
Correct |
1 ms |
1024 KB |
Output is correct |
12 |
Correct |
1 ms |
896 KB |
Output is correct |
13 |
Correct |
1 ms |
1024 KB |
Output is correct |
14 |
Correct |
1 ms |
896 KB |
Output is correct |
15 |
Correct |
1 ms |
896 KB |
Output is correct |
16 |
Correct |
1 ms |
896 KB |
Output is correct |
17 |
Correct |
1 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
1 ms |
896 KB |
Output is correct |
6 |
Correct |
1 ms |
896 KB |
Output is correct |
7 |
Correct |
1 ms |
896 KB |
Output is correct |
8 |
Correct |
1 ms |
896 KB |
Output is correct |
9 |
Correct |
1 ms |
896 KB |
Output is correct |
10 |
Correct |
1 ms |
1024 KB |
Output is correct |
11 |
Correct |
1 ms |
1024 KB |
Output is correct |
12 |
Correct |
1 ms |
896 KB |
Output is correct |
13 |
Correct |
1 ms |
1024 KB |
Output is correct |
14 |
Correct |
1 ms |
896 KB |
Output is correct |
15 |
Correct |
1 ms |
896 KB |
Output is correct |
16 |
Correct |
1 ms |
896 KB |
Output is correct |
17 |
Correct |
1 ms |
896 KB |
Output is correct |
18 |
Correct |
6 ms |
9088 KB |
Output is correct |
19 |
Correct |
5 ms |
8960 KB |
Output is correct |
20 |
Correct |
6 ms |
8960 KB |
Output is correct |
21 |
Correct |
6 ms |
8960 KB |
Output is correct |
22 |
Correct |
5 ms |
9088 KB |
Output is correct |
23 |
Correct |
6 ms |
8960 KB |
Output is correct |
24 |
Correct |
5 ms |
8960 KB |
Output is correct |
25 |
Correct |
5 ms |
9088 KB |
Output is correct |
26 |
Correct |
5 ms |
9088 KB |
Output is correct |
27 |
Correct |
5 ms |
8960 KB |
Output is correct |
28 |
Correct |
5 ms |
9088 KB |
Output is correct |
29 |
Correct |
5 ms |
8960 KB |
Output is correct |
30 |
Correct |
5 ms |
8960 KB |
Output is correct |
31 |
Correct |
6 ms |
8960 KB |
Output is correct |
32 |
Correct |
5 ms |
8960 KB |
Output is correct |
33 |
Correct |
5 ms |
8704 KB |
Output is correct |
34 |
Correct |
5 ms |
8704 KB |
Output is correct |
35 |
Correct |
5 ms |
8448 KB |
Output is correct |
36 |
Correct |
5 ms |
8704 KB |
Output is correct |
37 |
Correct |
5 ms |
8192 KB |
Output is correct |
38 |
Correct |
7 ms |
8960 KB |
Output is correct |
39 |
Correct |
6 ms |
8960 KB |
Output is correct |
40 |
Correct |
5 ms |
8704 KB |
Output is correct |
41 |
Correct |
5 ms |
8960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
236 ms |
384504 KB |
Output is correct |
3 |
Correct |
237 ms |
382712 KB |
Output is correct |
4 |
Correct |
238 ms |
384504 KB |
Output is correct |
5 |
Correct |
239 ms |
384504 KB |
Output is correct |
6 |
Correct |
236 ms |
384504 KB |
Output is correct |
7 |
Correct |
234 ms |
382584 KB |
Output is correct |
8 |
Correct |
235 ms |
382712 KB |
Output is correct |
9 |
Correct |
237 ms |
380664 KB |
Output is correct |
10 |
Correct |
238 ms |
384504 KB |
Output is correct |
11 |
Correct |
235 ms |
384632 KB |
Output is correct |
12 |
Correct |
64 ms |
102904 KB |
Output is correct |
13 |
Correct |
107 ms |
181236 KB |
Output is correct |
14 |
Correct |
158 ms |
262392 KB |
Output is correct |
15 |
Correct |
238 ms |
384492 KB |
Output is correct |
16 |
Correct |
239 ms |
384504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
1 ms |
896 KB |
Output is correct |
6 |
Correct |
1 ms |
896 KB |
Output is correct |
7 |
Correct |
1 ms |
896 KB |
Output is correct |
8 |
Correct |
1 ms |
896 KB |
Output is correct |
9 |
Correct |
1 ms |
896 KB |
Output is correct |
10 |
Correct |
1 ms |
1024 KB |
Output is correct |
11 |
Correct |
1 ms |
1024 KB |
Output is correct |
12 |
Correct |
1 ms |
896 KB |
Output is correct |
13 |
Correct |
1 ms |
1024 KB |
Output is correct |
14 |
Correct |
1 ms |
896 KB |
Output is correct |
15 |
Correct |
1 ms |
896 KB |
Output is correct |
16 |
Correct |
1 ms |
896 KB |
Output is correct |
17 |
Correct |
1 ms |
896 KB |
Output is correct |
18 |
Correct |
6 ms |
9088 KB |
Output is correct |
19 |
Correct |
5 ms |
8960 KB |
Output is correct |
20 |
Correct |
6 ms |
8960 KB |
Output is correct |
21 |
Correct |
6 ms |
8960 KB |
Output is correct |
22 |
Correct |
5 ms |
9088 KB |
Output is correct |
23 |
Correct |
6 ms |
8960 KB |
Output is correct |
24 |
Correct |
5 ms |
8960 KB |
Output is correct |
25 |
Correct |
5 ms |
9088 KB |
Output is correct |
26 |
Correct |
5 ms |
9088 KB |
Output is correct |
27 |
Correct |
5 ms |
8960 KB |
Output is correct |
28 |
Correct |
5 ms |
9088 KB |
Output is correct |
29 |
Correct |
5 ms |
8960 KB |
Output is correct |
30 |
Correct |
5 ms |
8960 KB |
Output is correct |
31 |
Correct |
6 ms |
8960 KB |
Output is correct |
32 |
Correct |
5 ms |
8960 KB |
Output is correct |
33 |
Correct |
5 ms |
8704 KB |
Output is correct |
34 |
Correct |
5 ms |
8704 KB |
Output is correct |
35 |
Correct |
5 ms |
8448 KB |
Output is correct |
36 |
Correct |
5 ms |
8704 KB |
Output is correct |
37 |
Correct |
5 ms |
8192 KB |
Output is correct |
38 |
Correct |
7 ms |
8960 KB |
Output is correct |
39 |
Correct |
6 ms |
8960 KB |
Output is correct |
40 |
Correct |
5 ms |
8704 KB |
Output is correct |
41 |
Correct |
5 ms |
8960 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
236 ms |
384504 KB |
Output is correct |
44 |
Correct |
237 ms |
382712 KB |
Output is correct |
45 |
Correct |
238 ms |
384504 KB |
Output is correct |
46 |
Correct |
239 ms |
384504 KB |
Output is correct |
47 |
Correct |
236 ms |
384504 KB |
Output is correct |
48 |
Correct |
234 ms |
382584 KB |
Output is correct |
49 |
Correct |
235 ms |
382712 KB |
Output is correct |
50 |
Correct |
237 ms |
380664 KB |
Output is correct |
51 |
Correct |
238 ms |
384504 KB |
Output is correct |
52 |
Correct |
235 ms |
384632 KB |
Output is correct |
53 |
Correct |
64 ms |
102904 KB |
Output is correct |
54 |
Correct |
107 ms |
181236 KB |
Output is correct |
55 |
Correct |
158 ms |
262392 KB |
Output is correct |
56 |
Correct |
238 ms |
384492 KB |
Output is correct |
57 |
Correct |
239 ms |
384504 KB |
Output is correct |
58 |
Correct |
466 ms |
384712 KB |
Output is correct |
59 |
Correct |
434 ms |
384676 KB |
Output is correct |
60 |
Correct |
437 ms |
380664 KB |
Output is correct |
61 |
Correct |
448 ms |
382584 KB |
Output is correct |
62 |
Correct |
247 ms |
384632 KB |
Output is correct |
63 |
Correct |
242 ms |
384504 KB |
Output is correct |
64 |
Correct |
276 ms |
384496 KB |
Output is correct |
65 |
Correct |
298 ms |
382584 KB |
Output is correct |
66 |
Correct |
348 ms |
378888 KB |
Output is correct |
67 |
Correct |
354 ms |
384504 KB |
Output is correct |
68 |
Correct |
344 ms |
382584 KB |
Output is correct |
69 |
Correct |
359 ms |
384504 KB |
Output is correct |
70 |
Correct |
363 ms |
384632 KB |
Output is correct |
71 |
Correct |
399 ms |
382588 KB |
Output is correct |
72 |
Correct |
252 ms |
382712 KB |
Output is correct |
73 |
Correct |
248 ms |
384504 KB |
Output is correct |