# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
121213 | 2019-06-26T08:12:01 Z | 윤교준(#2970) | Growing Vegetable is Fun 3 (JOI19_ho_t3) | C++14 | 166 ms | 4352 KB |
#include <bits/stdc++.h> #define eb emplace_back #define sz(V) ((int)(V).size()) #define upmin(a,b) (a)=min((a),(b)) #define INF (0x3f3f3f3f) using namespace std; const int MAXN = 405; int DP[2][MAXN][MAXN][3]; vector<int> AV[3]; int AC[MAXN][3]; int A[MAXN]; int N, Ans = INF; int f(int a, int b, int c, int v) { if(!v) v = AV[0][a]; else if(1 == v) v = AV[1][b]; else v = AV[2][c]; return max(0, AC[v-1][0]-a) + max(0, AC[v-1][1]-b) + max(0, AC[v-1][2]-c); } int main() { { char str[MAXN]; scanf("%d %s", &N, str+1); for(int i = 1; i <= N; i++) { if('R' == str[i]) A[i] = 1; if('G' == str[i]) A[i] = 2; } } for(int i = 1; i <= N; i++) AV[A[i]].eb(i); for(int i = 1; i <= N; i++) { for(int t = 0; t < 3; t++) { AC[i][t] = AC[i-1][t]; } AC[i][A[i]]++; } fill(DP[0][0][0], DP[1][MAXN-1][MAXN-1]+3, INF); if(!AV[0].empty()) DP[1][1][0][0] = AV[0][0] - 1; if(!AV[1].empty()) DP[1][0][1][1] = AV[1][0] - 1; if(!AV[2].empty()) DP[1][0][0][2] = AV[2][0] - 1; for(int l = 1; l < N; l++) { fill(DP[1^l&1][0][0], DP[1^l&1][MAXN-1][MAXN-1]+3, INF); for(int a = 0; a <= l && a <= sz(AV[0]); a++) { for(int b = 0; b <= l && b <= sz(AV[1]); b++) { int c = l-a-b; if(c < 0 || sz(AV[2]) < c) continue; for(int t = 0; t < 3; t++) { int ret = DP[l&1][a][b][t]; if(INF <= ret) continue; for(int v = 0; v < 3; v++) { if(t == v) continue; if(!v) { if(a == sz(AV[0])) continue; upmin(DP[1^l&1][a+1][b][0], ret + f(a, b, c, 0)); } else if(1 == v) { if(b == sz(AV[1])) continue; upmin(DP[1^l&1][a][b+1][1], ret + f(a, b, c, 1)); } else { if(c == sz(AV[2])) continue; upmin(DP[1^l&1][a][b][2], ret + f(a, b, c, 2)); } } } } } } for(int i = 0; i < 3; i++) upmin(Ans, DP[N&1][sz(AV[0])][sz(AV[1])][i]); cout << (INF <= Ans ? -1 : Ans) << endl; return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 4224 KB | Output is correct |
2 | Correct | 5 ms | 4096 KB | Output is correct |
3 | Correct | 5 ms | 4224 KB | Output is correct |
4 | Correct | 7 ms | 4224 KB | Output is correct |
5 | Correct | 8 ms | 4224 KB | Output is correct |
6 | Correct | 8 ms | 4224 KB | Output is correct |
7 | Correct | 8 ms | 4096 KB | Output is correct |
8 | Correct | 7 ms | 4096 KB | Output is correct |
9 | Correct | 9 ms | 4224 KB | Output is correct |
10 | Correct | 8 ms | 4224 KB | Output is correct |
11 | Correct | 8 ms | 4224 KB | Output is correct |
12 | Correct | 8 ms | 4224 KB | Output is correct |
13 | Correct | 8 ms | 4224 KB | Output is correct |
14 | Correct | 8 ms | 4224 KB | Output is correct |
15 | Correct | 8 ms | 4224 KB | Output is correct |
16 | Correct | 8 ms | 4224 KB | Output is correct |
17 | Correct | 8 ms | 4096 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 4224 KB | Output is correct |
2 | Correct | 5 ms | 4096 KB | Output is correct |
3 | Correct | 5 ms | 4224 KB | Output is correct |
4 | Correct | 7 ms | 4224 KB | Output is correct |
5 | Correct | 8 ms | 4224 KB | Output is correct |
6 | Correct | 8 ms | 4224 KB | Output is correct |
7 | Correct | 8 ms | 4096 KB | Output is correct |
8 | Correct | 7 ms | 4096 KB | Output is correct |
9 | Correct | 9 ms | 4224 KB | Output is correct |
10 | Correct | 8 ms | 4224 KB | Output is correct |
11 | Correct | 8 ms | 4224 KB | Output is correct |
12 | Correct | 8 ms | 4224 KB | Output is correct |
13 | Correct | 8 ms | 4224 KB | Output is correct |
14 | Correct | 8 ms | 4224 KB | Output is correct |
15 | Correct | 8 ms | 4224 KB | Output is correct |
16 | Correct | 8 ms | 4224 KB | Output is correct |
17 | Correct | 8 ms | 4096 KB | Output is correct |
18 | Correct | 20 ms | 4096 KB | Output is correct |
19 | Correct | 18 ms | 4224 KB | Output is correct |
20 | Correct | 20 ms | 4224 KB | Output is correct |
21 | Correct | 19 ms | 4216 KB | Output is correct |
22 | Correct | 20 ms | 4224 KB | Output is correct |
23 | Correct | 21 ms | 4128 KB | Output is correct |
24 | Correct | 17 ms | 4224 KB | Output is correct |
25 | Correct | 18 ms | 4224 KB | Output is correct |
26 | Correct | 17 ms | 4216 KB | Output is correct |
27 | Correct | 18 ms | 4224 KB | Output is correct |
28 | Correct | 18 ms | 4096 KB | Output is correct |
29 | Correct | 18 ms | 4224 KB | Output is correct |
30 | Correct | 18 ms | 4224 KB | Output is correct |
31 | Correct | 19 ms | 4096 KB | Output is correct |
32 | Correct | 18 ms | 4224 KB | Output is correct |
33 | Correct | 18 ms | 4224 KB | Output is correct |
34 | Correct | 18 ms | 4224 KB | Output is correct |
35 | Correct | 18 ms | 4224 KB | Output is correct |
36 | Correct | 18 ms | 4224 KB | Output is correct |
37 | Correct | 17 ms | 4224 KB | Output is correct |
38 | Correct | 18 ms | 4224 KB | Output is correct |
39 | Correct | 18 ms | 4224 KB | Output is correct |
40 | Correct | 17 ms | 4352 KB | Output is correct |
41 | Correct | 19 ms | 4348 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 4224 KB | Output is correct |
2 | Correct | 94 ms | 4216 KB | Output is correct |
3 | Correct | 95 ms | 4224 KB | Output is correct |
4 | Correct | 96 ms | 4224 KB | Output is correct |
5 | Correct | 90 ms | 4216 KB | Output is correct |
6 | Correct | 89 ms | 4224 KB | Output is correct |
7 | Correct | 95 ms | 4344 KB | Output is correct |
8 | Correct | 97 ms | 4216 KB | Output is correct |
9 | Correct | 89 ms | 4224 KB | Output is correct |
10 | Correct | 93 ms | 4248 KB | Output is correct |
11 | Correct | 94 ms | 4216 KB | Output is correct |
12 | Correct | 54 ms | 4344 KB | Output is correct |
13 | Correct | 67 ms | 4216 KB | Output is correct |
14 | Correct | 81 ms | 4224 KB | Output is correct |
15 | Correct | 94 ms | 4344 KB | Output is correct |
16 | Correct | 96 ms | 4216 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 4224 KB | Output is correct |
2 | Correct | 5 ms | 4096 KB | Output is correct |
3 | Correct | 5 ms | 4224 KB | Output is correct |
4 | Correct | 7 ms | 4224 KB | Output is correct |
5 | Correct | 8 ms | 4224 KB | Output is correct |
6 | Correct | 8 ms | 4224 KB | Output is correct |
7 | Correct | 8 ms | 4096 KB | Output is correct |
8 | Correct | 7 ms | 4096 KB | Output is correct |
9 | Correct | 9 ms | 4224 KB | Output is correct |
10 | Correct | 8 ms | 4224 KB | Output is correct |
11 | Correct | 8 ms | 4224 KB | Output is correct |
12 | Correct | 8 ms | 4224 KB | Output is correct |
13 | Correct | 8 ms | 4224 KB | Output is correct |
14 | Correct | 8 ms | 4224 KB | Output is correct |
15 | Correct | 8 ms | 4224 KB | Output is correct |
16 | Correct | 8 ms | 4224 KB | Output is correct |
17 | Correct | 8 ms | 4096 KB | Output is correct |
18 | Correct | 20 ms | 4096 KB | Output is correct |
19 | Correct | 18 ms | 4224 KB | Output is correct |
20 | Correct | 20 ms | 4224 KB | Output is correct |
21 | Correct | 19 ms | 4216 KB | Output is correct |
22 | Correct | 20 ms | 4224 KB | Output is correct |
23 | Correct | 21 ms | 4128 KB | Output is correct |
24 | Correct | 17 ms | 4224 KB | Output is correct |
25 | Correct | 18 ms | 4224 KB | Output is correct |
26 | Correct | 17 ms | 4216 KB | Output is correct |
27 | Correct | 18 ms | 4224 KB | Output is correct |
28 | Correct | 18 ms | 4096 KB | Output is correct |
29 | Correct | 18 ms | 4224 KB | Output is correct |
30 | Correct | 18 ms | 4224 KB | Output is correct |
31 | Correct | 19 ms | 4096 KB | Output is correct |
32 | Correct | 18 ms | 4224 KB | Output is correct |
33 | Correct | 18 ms | 4224 KB | Output is correct |
34 | Correct | 18 ms | 4224 KB | Output is correct |
35 | Correct | 18 ms | 4224 KB | Output is correct |
36 | Correct | 18 ms | 4224 KB | Output is correct |
37 | Correct | 17 ms | 4224 KB | Output is correct |
38 | Correct | 18 ms | 4224 KB | Output is correct |
39 | Correct | 18 ms | 4224 KB | Output is correct |
40 | Correct | 17 ms | 4352 KB | Output is correct |
41 | Correct | 19 ms | 4348 KB | Output is correct |
42 | Correct | 6 ms | 4224 KB | Output is correct |
43 | Correct | 94 ms | 4216 KB | Output is correct |
44 | Correct | 95 ms | 4224 KB | Output is correct |
45 | Correct | 96 ms | 4224 KB | Output is correct |
46 | Correct | 90 ms | 4216 KB | Output is correct |
47 | Correct | 89 ms | 4224 KB | Output is correct |
48 | Correct | 95 ms | 4344 KB | Output is correct |
49 | Correct | 97 ms | 4216 KB | Output is correct |
50 | Correct | 89 ms | 4224 KB | Output is correct |
51 | Correct | 93 ms | 4248 KB | Output is correct |
52 | Correct | 94 ms | 4216 KB | Output is correct |
53 | Correct | 54 ms | 4344 KB | Output is correct |
54 | Correct | 67 ms | 4216 KB | Output is correct |
55 | Correct | 81 ms | 4224 KB | Output is correct |
56 | Correct | 94 ms | 4344 KB | Output is correct |
57 | Correct | 96 ms | 4216 KB | Output is correct |
58 | Correct | 157 ms | 4216 KB | Output is correct |
59 | Correct | 156 ms | 4216 KB | Output is correct |
60 | Correct | 155 ms | 4316 KB | Output is correct |
61 | Correct | 159 ms | 4216 KB | Output is correct |
62 | Correct | 96 ms | 4248 KB | Output is correct |
63 | Correct | 97 ms | 4224 KB | Output is correct |
64 | Correct | 107 ms | 4224 KB | Output is correct |
65 | Correct | 124 ms | 4224 KB | Output is correct |
66 | Correct | 155 ms | 4216 KB | Output is correct |
67 | Correct | 166 ms | 4212 KB | Output is correct |
68 | Correct | 154 ms | 4216 KB | Output is correct |
69 | Correct | 157 ms | 4224 KB | Output is correct |
70 | Correct | 152 ms | 4224 KB | Output is correct |
71 | Correct | 145 ms | 4256 KB | Output is correct |
72 | Correct | 98 ms | 4216 KB | Output is correct |
73 | Correct | 96 ms | 4224 KB | Output is correct |