#include <bits/stdc++.h>
using namespace std;
#define minim(X, Y) (X = min(X, Y))
#define F(X, Y) for(int X = 0; X < Y; X++)
#define M(X) max(X, 0)
const int Z = 202, INF = 1e9;
int N, A[Z*2], DP[Z][Z][Z][3], p[3][Z*2], h[3][Z*2], c[3], x, y, z;
int main() {
cin >> N, getchar();
F(i, N) {
char v = getchar();
A[i] = (v < 82) + (v < 89);
}
F(i, Z) F(j, Z) F(k, Z) F(l, 3)
DP[i][j][k][l] = (i || j || k) ? INF : 0;
F(i, N) {
if(++c[A[i]] >= Z-1) break;
p[A[i]][c[A[i]]] = i;
F(j, 3) h[j][i] = c[j];
F(j, i+1) F(k, i+1) {
if(A[i] == 0) x = c[0], y = j, z = k;
if(A[i] == 1) x = j, y = c[1], z = k;
if(A[i] == 2) x = j, y = k, z = c[2];
if(x > c[0] || y > c[1] || z > c[2]) continue;
int s = x + y + z;
if(x) minim(DP[x][y][z][0], min(DP[x-1][y][z][1], DP[x-1][y][z][2]) + M(y-h[1][p[0][x]]) + M(z-h[2][p[0][x]]));
if(y) minim(DP[x][y][z][1], min(DP[x][y-1][z][0], DP[x][y-1][z][2]) + M(x-h[0][p[1][y]]) + M(z-h[2][p[1][y]]));
if(z) minim(DP[x][y][z][2], min(DP[x][y][z-1][0], DP[x][y][z-1][1]) + M(x-h[0][p[2][z]]) + M(y-h[1][p[2][z]]));
}
}
int res = INF;
F(i, 3) minim(res, DP[c[0]][c[1]][c[2]][i]);
cout << (res == INF ? -1 : res);
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:31:8: warning: unused variable 's' [-Wunused-variable]
31 | int s = x + y + z;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
97020 KB |
Output is correct |
2 |
Correct |
43 ms |
96984 KB |
Output is correct |
3 |
Correct |
42 ms |
97012 KB |
Output is correct |
4 |
Correct |
42 ms |
96988 KB |
Output is correct |
5 |
Correct |
43 ms |
97048 KB |
Output is correct |
6 |
Correct |
43 ms |
97008 KB |
Output is correct |
7 |
Correct |
43 ms |
96964 KB |
Output is correct |
8 |
Correct |
43 ms |
97000 KB |
Output is correct |
9 |
Correct |
43 ms |
97048 KB |
Output is correct |
10 |
Correct |
43 ms |
97152 KB |
Output is correct |
11 |
Correct |
43 ms |
97096 KB |
Output is correct |
12 |
Correct |
42 ms |
96964 KB |
Output is correct |
13 |
Correct |
43 ms |
96964 KB |
Output is correct |
14 |
Correct |
43 ms |
96984 KB |
Output is correct |
15 |
Correct |
43 ms |
96960 KB |
Output is correct |
16 |
Correct |
45 ms |
96964 KB |
Output is correct |
17 |
Correct |
43 ms |
97060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
97020 KB |
Output is correct |
2 |
Correct |
43 ms |
96984 KB |
Output is correct |
3 |
Correct |
42 ms |
97012 KB |
Output is correct |
4 |
Correct |
42 ms |
96988 KB |
Output is correct |
5 |
Correct |
43 ms |
97048 KB |
Output is correct |
6 |
Correct |
43 ms |
97008 KB |
Output is correct |
7 |
Correct |
43 ms |
96964 KB |
Output is correct |
8 |
Correct |
43 ms |
97000 KB |
Output is correct |
9 |
Correct |
43 ms |
97048 KB |
Output is correct |
10 |
Correct |
43 ms |
97152 KB |
Output is correct |
11 |
Correct |
43 ms |
97096 KB |
Output is correct |
12 |
Correct |
42 ms |
96964 KB |
Output is correct |
13 |
Correct |
43 ms |
96964 KB |
Output is correct |
14 |
Correct |
43 ms |
96984 KB |
Output is correct |
15 |
Correct |
43 ms |
96960 KB |
Output is correct |
16 |
Correct |
45 ms |
96964 KB |
Output is correct |
17 |
Correct |
43 ms |
97060 KB |
Output is correct |
18 |
Correct |
43 ms |
96956 KB |
Output is correct |
19 |
Correct |
48 ms |
97032 KB |
Output is correct |
20 |
Correct |
45 ms |
97016 KB |
Output is correct |
21 |
Correct |
48 ms |
96972 KB |
Output is correct |
22 |
Correct |
43 ms |
97072 KB |
Output is correct |
23 |
Correct |
42 ms |
97040 KB |
Output is correct |
24 |
Correct |
43 ms |
96964 KB |
Output is correct |
25 |
Correct |
44 ms |
97096 KB |
Output is correct |
26 |
Correct |
43 ms |
97060 KB |
Output is correct |
27 |
Correct |
42 ms |
97012 KB |
Output is correct |
28 |
Correct |
42 ms |
97008 KB |
Output is correct |
29 |
Correct |
44 ms |
96972 KB |
Output is correct |
30 |
Correct |
43 ms |
96948 KB |
Output is correct |
31 |
Correct |
43 ms |
97012 KB |
Output is correct |
32 |
Correct |
43 ms |
96964 KB |
Output is correct |
33 |
Correct |
43 ms |
97024 KB |
Output is correct |
34 |
Correct |
44 ms |
97056 KB |
Output is correct |
35 |
Correct |
43 ms |
96964 KB |
Output is correct |
36 |
Correct |
43 ms |
96992 KB |
Output is correct |
37 |
Correct |
46 ms |
97004 KB |
Output is correct |
38 |
Correct |
43 ms |
96964 KB |
Output is correct |
39 |
Correct |
44 ms |
97112 KB |
Output is correct |
40 |
Correct |
44 ms |
96960 KB |
Output is correct |
41 |
Correct |
43 ms |
96996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
97036 KB |
Output is correct |
2 |
Correct |
72 ms |
96992 KB |
Output is correct |
3 |
Correct |
71 ms |
97004 KB |
Output is correct |
4 |
Correct |
69 ms |
96968 KB |
Output is correct |
5 |
Correct |
71 ms |
97064 KB |
Output is correct |
6 |
Correct |
67 ms |
97060 KB |
Output is correct |
7 |
Correct |
66 ms |
97016 KB |
Output is correct |
8 |
Correct |
66 ms |
96988 KB |
Output is correct |
9 |
Correct |
78 ms |
96996 KB |
Output is correct |
10 |
Correct |
69 ms |
96964 KB |
Output is correct |
11 |
Correct |
70 ms |
96964 KB |
Output is correct |
12 |
Correct |
52 ms |
97060 KB |
Output is correct |
13 |
Correct |
49 ms |
96972 KB |
Output is correct |
14 |
Correct |
60 ms |
97024 KB |
Output is correct |
15 |
Correct |
82 ms |
97048 KB |
Output is correct |
16 |
Correct |
67 ms |
97044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
97020 KB |
Output is correct |
2 |
Correct |
43 ms |
96984 KB |
Output is correct |
3 |
Correct |
42 ms |
97012 KB |
Output is correct |
4 |
Correct |
42 ms |
96988 KB |
Output is correct |
5 |
Correct |
43 ms |
97048 KB |
Output is correct |
6 |
Correct |
43 ms |
97008 KB |
Output is correct |
7 |
Correct |
43 ms |
96964 KB |
Output is correct |
8 |
Correct |
43 ms |
97000 KB |
Output is correct |
9 |
Correct |
43 ms |
97048 KB |
Output is correct |
10 |
Correct |
43 ms |
97152 KB |
Output is correct |
11 |
Correct |
43 ms |
97096 KB |
Output is correct |
12 |
Correct |
42 ms |
96964 KB |
Output is correct |
13 |
Correct |
43 ms |
96964 KB |
Output is correct |
14 |
Correct |
43 ms |
96984 KB |
Output is correct |
15 |
Correct |
43 ms |
96960 KB |
Output is correct |
16 |
Correct |
45 ms |
96964 KB |
Output is correct |
17 |
Correct |
43 ms |
97060 KB |
Output is correct |
18 |
Correct |
43 ms |
96956 KB |
Output is correct |
19 |
Correct |
48 ms |
97032 KB |
Output is correct |
20 |
Correct |
45 ms |
97016 KB |
Output is correct |
21 |
Correct |
48 ms |
96972 KB |
Output is correct |
22 |
Correct |
43 ms |
97072 KB |
Output is correct |
23 |
Correct |
42 ms |
97040 KB |
Output is correct |
24 |
Correct |
43 ms |
96964 KB |
Output is correct |
25 |
Correct |
44 ms |
97096 KB |
Output is correct |
26 |
Correct |
43 ms |
97060 KB |
Output is correct |
27 |
Correct |
42 ms |
97012 KB |
Output is correct |
28 |
Correct |
42 ms |
97008 KB |
Output is correct |
29 |
Correct |
44 ms |
96972 KB |
Output is correct |
30 |
Correct |
43 ms |
96948 KB |
Output is correct |
31 |
Correct |
43 ms |
97012 KB |
Output is correct |
32 |
Correct |
43 ms |
96964 KB |
Output is correct |
33 |
Correct |
43 ms |
97024 KB |
Output is correct |
34 |
Correct |
44 ms |
97056 KB |
Output is correct |
35 |
Correct |
43 ms |
96964 KB |
Output is correct |
36 |
Correct |
43 ms |
96992 KB |
Output is correct |
37 |
Correct |
46 ms |
97004 KB |
Output is correct |
38 |
Correct |
43 ms |
96964 KB |
Output is correct |
39 |
Correct |
44 ms |
97112 KB |
Output is correct |
40 |
Correct |
44 ms |
96960 KB |
Output is correct |
41 |
Correct |
43 ms |
96996 KB |
Output is correct |
42 |
Correct |
43 ms |
97036 KB |
Output is correct |
43 |
Correct |
72 ms |
96992 KB |
Output is correct |
44 |
Correct |
71 ms |
97004 KB |
Output is correct |
45 |
Correct |
69 ms |
96968 KB |
Output is correct |
46 |
Correct |
71 ms |
97064 KB |
Output is correct |
47 |
Correct |
67 ms |
97060 KB |
Output is correct |
48 |
Correct |
66 ms |
97016 KB |
Output is correct |
49 |
Correct |
66 ms |
96988 KB |
Output is correct |
50 |
Correct |
78 ms |
96996 KB |
Output is correct |
51 |
Correct |
69 ms |
96964 KB |
Output is correct |
52 |
Correct |
70 ms |
96964 KB |
Output is correct |
53 |
Correct |
52 ms |
97060 KB |
Output is correct |
54 |
Correct |
49 ms |
96972 KB |
Output is correct |
55 |
Correct |
60 ms |
97024 KB |
Output is correct |
56 |
Correct |
82 ms |
97048 KB |
Output is correct |
57 |
Correct |
67 ms |
97044 KB |
Output is correct |
58 |
Correct |
101 ms |
97068 KB |
Output is correct |
59 |
Correct |
105 ms |
97000 KB |
Output is correct |
60 |
Correct |
100 ms |
96984 KB |
Output is correct |
61 |
Correct |
105 ms |
97068 KB |
Output is correct |
62 |
Correct |
79 ms |
97076 KB |
Output is correct |
63 |
Correct |
85 ms |
97044 KB |
Output is correct |
64 |
Correct |
84 ms |
97068 KB |
Output is correct |
65 |
Correct |
108 ms |
97064 KB |
Output is correct |
66 |
Correct |
116 ms |
97052 KB |
Output is correct |
67 |
Correct |
107 ms |
97068 KB |
Output is correct |
68 |
Correct |
109 ms |
97064 KB |
Output is correct |
69 |
Correct |
115 ms |
97024 KB |
Output is correct |
70 |
Correct |
125 ms |
96948 KB |
Output is correct |
71 |
Correct |
112 ms |
97024 KB |
Output is correct |
72 |
Correct |
52 ms |
96944 KB |
Output is correct |
73 |
Correct |
52 ms |
97036 KB |
Output is correct |