#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
using namespace std;
const int N = 401;
const int inf = int(1e9) + 7;
int c[N][3], pos[N][3], f[N][N][N][3];
int n, step, p;
string s;
void Irene(int r, int g, int y, int cur, int col) {
if(col == 0) {
p = pos[r + 1][col];
step = max(c[p][1] - g, 0) + max(c[p][2] - y, 0);
f[r + 1][g][y][col] = min(f[r + 1][g][y][col], f[r][g][y][cur] + step);
} else if(col == 1) {
p = pos[g + 1][col];
step = max(c[p][0] - r, 0) + max(c[p][2] - y, 0);
f[r][g + 1][y][col] = min(f[r][g + 1][y][col], f[r][g][y][cur] + step);
} else {
p = pos[y + 1][col];
step = max(c[p][0] - r, 0) + max(c[p][1] - g, 0);
f[r][g][y + 1][col] = min(f[r][g][y + 1][col], f[r][g][y][cur] + step);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
if(fopen("test.inp", "r")) {
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
cin >> n >> s;
s = ' ' + s;
for(int i = 1; i <= n; ++i) {
c[i][0] = c[i - 1][0], c[i][1] = c[i - 1][1], c[i][2] = c[i - 1][2];
if(s[i] == 'R') pos[++c[i][0]][0] = i;
else if(s[i] == 'G') pos[++c[i][1]][1] = i;
else pos[++c[i][2]][2] = i;
}
int res = inf, R = c[n][0], G = c[n][1], Y = c[n][2];
for(int i = 0; i <= R; ++i) {
for(int j = 0; j <= G; ++j) {
for(int k = 0; k <= Y; ++k) {
f[i][j][k][0] = f[i][j][k][1] = f[i][j][k][2] = inf;
}
}
}
f[0][0][0][0] = f[0][0][0][1] = f[0][0][0][2] = 0;
for(int i = 0; i <= R; ++i) {
for(int j = 0; j <= G; ++j) {
for(int k = 0; k <= Y; ++k) {
if(i < R) Irene(i, j, k, 1, 0), Irene(i, j, k, 2, 0);
if(j < G) Irene(i, j, k, 0, 1), Irene(i, j, k, 2, 1);
if(k < Y) Irene(i, j, k, 0, 2), Irene(i, j, k, 1, 2);
}
}
}
for(int i = 0; i < 3; ++i) res = min(res, f[R][G][Y][i]);
if(res == inf) return cout << -1, 0;
cout << res;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("test.inp", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("test.out", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
632 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
508 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
632 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
508 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
2404 KB |
Output is correct |
19 |
Correct |
4 ms |
2040 KB |
Output is correct |
20 |
Correct |
3 ms |
2296 KB |
Output is correct |
21 |
Correct |
4 ms |
2424 KB |
Output is correct |
22 |
Correct |
3 ms |
1756 KB |
Output is correct |
23 |
Correct |
3 ms |
2168 KB |
Output is correct |
24 |
Correct |
3 ms |
1656 KB |
Output is correct |
25 |
Correct |
5 ms |
3960 KB |
Output is correct |
26 |
Correct |
7 ms |
4216 KB |
Output is correct |
27 |
Correct |
4 ms |
3064 KB |
Output is correct |
28 |
Correct |
4 ms |
2424 KB |
Output is correct |
29 |
Correct |
4 ms |
2296 KB |
Output is correct |
30 |
Correct |
4 ms |
2296 KB |
Output is correct |
31 |
Correct |
4 ms |
2040 KB |
Output is correct |
32 |
Correct |
4 ms |
2424 KB |
Output is correct |
33 |
Correct |
5 ms |
3832 KB |
Output is correct |
34 |
Correct |
5 ms |
3576 KB |
Output is correct |
35 |
Correct |
4 ms |
2808 KB |
Output is correct |
36 |
Correct |
3 ms |
1912 KB |
Output is correct |
37 |
Correct |
3 ms |
1784 KB |
Output is correct |
38 |
Correct |
4 ms |
2168 KB |
Output is correct |
39 |
Correct |
4 ms |
2424 KB |
Output is correct |
40 |
Correct |
2 ms |
508 KB |
Output is correct |
41 |
Correct |
4 ms |
2300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
129 ms |
162944 KB |
Output is correct |
3 |
Correct |
131 ms |
162152 KB |
Output is correct |
4 |
Correct |
130 ms |
162936 KB |
Output is correct |
5 |
Correct |
130 ms |
162940 KB |
Output is correct |
6 |
Correct |
130 ms |
163016 KB |
Output is correct |
7 |
Correct |
130 ms |
162300 KB |
Output is correct |
8 |
Correct |
129 ms |
162228 KB |
Output is correct |
9 |
Correct |
129 ms |
161484 KB |
Output is correct |
10 |
Correct |
129 ms |
162936 KB |
Output is correct |
11 |
Correct |
130 ms |
162960 KB |
Output is correct |
12 |
Correct |
37 ms |
44024 KB |
Output is correct |
13 |
Correct |
64 ms |
77148 KB |
Output is correct |
14 |
Correct |
90 ms |
111376 KB |
Output is correct |
15 |
Correct |
130 ms |
163040 KB |
Output is correct |
16 |
Correct |
130 ms |
162936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
632 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
508 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
2404 KB |
Output is correct |
19 |
Correct |
4 ms |
2040 KB |
Output is correct |
20 |
Correct |
3 ms |
2296 KB |
Output is correct |
21 |
Correct |
4 ms |
2424 KB |
Output is correct |
22 |
Correct |
3 ms |
1756 KB |
Output is correct |
23 |
Correct |
3 ms |
2168 KB |
Output is correct |
24 |
Correct |
3 ms |
1656 KB |
Output is correct |
25 |
Correct |
5 ms |
3960 KB |
Output is correct |
26 |
Correct |
7 ms |
4216 KB |
Output is correct |
27 |
Correct |
4 ms |
3064 KB |
Output is correct |
28 |
Correct |
4 ms |
2424 KB |
Output is correct |
29 |
Correct |
4 ms |
2296 KB |
Output is correct |
30 |
Correct |
4 ms |
2296 KB |
Output is correct |
31 |
Correct |
4 ms |
2040 KB |
Output is correct |
32 |
Correct |
4 ms |
2424 KB |
Output is correct |
33 |
Correct |
5 ms |
3832 KB |
Output is correct |
34 |
Correct |
5 ms |
3576 KB |
Output is correct |
35 |
Correct |
4 ms |
2808 KB |
Output is correct |
36 |
Correct |
3 ms |
1912 KB |
Output is correct |
37 |
Correct |
3 ms |
1784 KB |
Output is correct |
38 |
Correct |
4 ms |
2168 KB |
Output is correct |
39 |
Correct |
4 ms |
2424 KB |
Output is correct |
40 |
Correct |
2 ms |
508 KB |
Output is correct |
41 |
Correct |
4 ms |
2300 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |
43 |
Correct |
129 ms |
162944 KB |
Output is correct |
44 |
Correct |
131 ms |
162152 KB |
Output is correct |
45 |
Correct |
130 ms |
162936 KB |
Output is correct |
46 |
Correct |
130 ms |
162940 KB |
Output is correct |
47 |
Correct |
130 ms |
163016 KB |
Output is correct |
48 |
Correct |
130 ms |
162300 KB |
Output is correct |
49 |
Correct |
129 ms |
162228 KB |
Output is correct |
50 |
Correct |
129 ms |
161484 KB |
Output is correct |
51 |
Correct |
129 ms |
162936 KB |
Output is correct |
52 |
Correct |
130 ms |
162960 KB |
Output is correct |
53 |
Correct |
37 ms |
44024 KB |
Output is correct |
54 |
Correct |
64 ms |
77148 KB |
Output is correct |
55 |
Correct |
90 ms |
111376 KB |
Output is correct |
56 |
Correct |
130 ms |
163040 KB |
Output is correct |
57 |
Correct |
130 ms |
162936 KB |
Output is correct |
58 |
Correct |
91 ms |
75512 KB |
Output is correct |
59 |
Correct |
107 ms |
95708 KB |
Output is correct |
60 |
Correct |
100 ms |
87032 KB |
Output is correct |
61 |
Correct |
98 ms |
84124 KB |
Output is correct |
62 |
Correct |
133 ms |
160760 KB |
Output is correct |
63 |
Correct |
134 ms |
160376 KB |
Output is correct |
64 |
Correct |
137 ms |
150164 KB |
Output is correct |
65 |
Correct |
136 ms |
138588 KB |
Output is correct |
66 |
Correct |
99 ms |
85340 KB |
Output is correct |
67 |
Correct |
95 ms |
82172 KB |
Output is correct |
68 |
Correct |
100 ms |
87800 KB |
Output is correct |
69 |
Correct |
98 ms |
84728 KB |
Output is correct |
70 |
Correct |
105 ms |
91640 KB |
Output is correct |
71 |
Correct |
96 ms |
83192 KB |
Output is correct |
72 |
Correct |
43 ms |
41208 KB |
Output is correct |
73 |
Correct |
6 ms |
2936 KB |
Output is correct |