#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n,m,k,x,y,z;
int dp[405][405][405][5];
int pos[405][5], cnt[5];
int val['z'];
char ar[405];
void cal(int r, int g, int y, int l) {
bool debug = 0;
if (r == 2 && !g && y == 1 && l == 2) debug = 1;
if (r > cnt[1] + (l == 1)) return;
if (g > cnt[2] + (l == 2)) return;
if (y > cnt[3] + (l == 3)) return;
int cur = pos[(l == 1 ? r : (l == 2 ? g : y))+1][l];
int R = 0, G = 0, Y = 0, ans = 0;
for (int i = 1; i <= n; i++) {
if (i == cur) continue;
if (ar[i] == 'R' && r) {
r--;
R++;
if (i > cur) ans++;
} else if (ar[i] == 'G' && g) {
g--;
G++;
if (i > cur) ans++;
} else if (ar[i] == 'Y' && y) {
y--;
Y++;
if (i > cur) ans++;
}
}
if (r || g || y) return;
int tmp = 1e9;
if (R && l != 1) tmp = min(tmp,dp[R-1][G][Y][1]);
if (G && l != 2) tmp = min(tmp,dp[R][G-1][Y][2]);
if (Y && l != 3) tmp = min(tmp,dp[R][G][Y-1][3]);
if (!R && !G && !Y) tmp = 0;
dp[R][G][Y][l] = ans + tmp;
}
int main() {
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
val['R'] = 1, val['G'] = 2, val['Y'] = 3;
cin >> n;
for (int i = 1; i <= n; i++) cin >> ar[i];
for (int i = 1; i <= n; i++) {
cnt[val[ar[i]]]++;
pos[cnt[val[ar[i]]]][val[ar[i]]] = i;
}
for (int r = 0; r <= cnt[1]; r++) {
for (int g = 0; g <= cnt[2]; g++) {
for (int y = 0; y <= cnt[3]; y++) {
for (int l = 1; l <= 3; l++) {
dp[r][g][y][l] = 1e9;
cal(r,g,y,l);
}
}
}
}
int tmp = 1e9;
if (cnt[1]) tmp = min(tmp,dp[cnt[1]-1][cnt[2]][cnt[3]][1]);
if (cnt[2]) tmp = min(tmp,dp[cnt[1]][cnt[2]-1][cnt[3]][2]);
if (cnt[3]) tmp = min(tmp,dp[cnt[1]][cnt[2]][cnt[3]-1][3]);
if (tmp >= 1e8) cout << "-1\n";
else cout << tmp << "\n";
}
Compilation message
joi2019_ho_t3.cpp: In function 'void cal(int, int, int, int)':
joi2019_ho_t3.cpp:12:10: warning: variable 'debug' set but not used [-Wunused-but-set-variable]
12 | bool debug = 0;
| ^~~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:50:21: warning: array subscript has type 'char' [-Wchar-subscripts]
50 | cnt[val[ar[i]]]++;
| ~~~~^
joi2019_ho_t3.cpp:51:25: warning: array subscript has type 'char' [-Wchar-subscripts]
51 | pos[cnt[val[ar[i]]]][val[ar[i]]] = i;
| ~~~~^
joi2019_ho_t3.cpp:51:38: warning: array subscript has type 'char' [-Wchar-subscripts]
51 | pos[cnt[val[ar[i]]]][val[ar[i]]] = i;
| ~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
456 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
456 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
6 ms |
2388 KB |
Output is correct |
19 |
Correct |
7 ms |
2004 KB |
Output is correct |
20 |
Correct |
5 ms |
2260 KB |
Output is correct |
21 |
Correct |
5 ms |
2516 KB |
Output is correct |
22 |
Correct |
5 ms |
1740 KB |
Output is correct |
23 |
Correct |
5 ms |
2260 KB |
Output is correct |
24 |
Correct |
4 ms |
1620 KB |
Output is correct |
25 |
Correct |
3 ms |
3924 KB |
Output is correct |
26 |
Correct |
3 ms |
4180 KB |
Output is correct |
27 |
Correct |
5 ms |
3120 KB |
Output is correct |
28 |
Correct |
5 ms |
2388 KB |
Output is correct |
29 |
Correct |
6 ms |
2260 KB |
Output is correct |
30 |
Correct |
6 ms |
2508 KB |
Output is correct |
31 |
Correct |
5 ms |
2120 KB |
Output is correct |
32 |
Correct |
6 ms |
2508 KB |
Output is correct |
33 |
Correct |
3 ms |
3924 KB |
Output is correct |
34 |
Correct |
3 ms |
3540 KB |
Output is correct |
35 |
Correct |
4 ms |
2772 KB |
Output is correct |
36 |
Correct |
5 ms |
2004 KB |
Output is correct |
37 |
Correct |
4 ms |
1748 KB |
Output is correct |
38 |
Correct |
5 ms |
2248 KB |
Output is correct |
39 |
Correct |
5 ms |
2388 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
3 ms |
2260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
134 ms |
163408 KB |
Output is correct |
3 |
Correct |
132 ms |
162612 KB |
Output is correct |
4 |
Correct |
158 ms |
163384 KB |
Output is correct |
5 |
Correct |
155 ms |
163488 KB |
Output is correct |
6 |
Correct |
176 ms |
163472 KB |
Output is correct |
7 |
Correct |
159 ms |
162680 KB |
Output is correct |
8 |
Correct |
163 ms |
162752 KB |
Output is correct |
9 |
Correct |
170 ms |
161868 KB |
Output is correct |
10 |
Correct |
164 ms |
163468 KB |
Output is correct |
11 |
Correct |
157 ms |
163492 KB |
Output is correct |
12 |
Correct |
32 ms |
44196 KB |
Output is correct |
13 |
Correct |
63 ms |
77472 KB |
Output is correct |
14 |
Correct |
100 ms |
111680 KB |
Output is correct |
15 |
Correct |
156 ms |
163456 KB |
Output is correct |
16 |
Correct |
159 ms |
163460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
456 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
6 ms |
2388 KB |
Output is correct |
19 |
Correct |
7 ms |
2004 KB |
Output is correct |
20 |
Correct |
5 ms |
2260 KB |
Output is correct |
21 |
Correct |
5 ms |
2516 KB |
Output is correct |
22 |
Correct |
5 ms |
1740 KB |
Output is correct |
23 |
Correct |
5 ms |
2260 KB |
Output is correct |
24 |
Correct |
4 ms |
1620 KB |
Output is correct |
25 |
Correct |
3 ms |
3924 KB |
Output is correct |
26 |
Correct |
3 ms |
4180 KB |
Output is correct |
27 |
Correct |
5 ms |
3120 KB |
Output is correct |
28 |
Correct |
5 ms |
2388 KB |
Output is correct |
29 |
Correct |
6 ms |
2260 KB |
Output is correct |
30 |
Correct |
6 ms |
2508 KB |
Output is correct |
31 |
Correct |
5 ms |
2120 KB |
Output is correct |
32 |
Correct |
6 ms |
2508 KB |
Output is correct |
33 |
Correct |
3 ms |
3924 KB |
Output is correct |
34 |
Correct |
3 ms |
3540 KB |
Output is correct |
35 |
Correct |
4 ms |
2772 KB |
Output is correct |
36 |
Correct |
5 ms |
2004 KB |
Output is correct |
37 |
Correct |
4 ms |
1748 KB |
Output is correct |
38 |
Correct |
5 ms |
2248 KB |
Output is correct |
39 |
Correct |
5 ms |
2388 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
3 ms |
2260 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
134 ms |
163408 KB |
Output is correct |
44 |
Correct |
132 ms |
162612 KB |
Output is correct |
45 |
Correct |
158 ms |
163384 KB |
Output is correct |
46 |
Correct |
155 ms |
163488 KB |
Output is correct |
47 |
Correct |
176 ms |
163472 KB |
Output is correct |
48 |
Correct |
159 ms |
162680 KB |
Output is correct |
49 |
Correct |
163 ms |
162752 KB |
Output is correct |
50 |
Correct |
170 ms |
161868 KB |
Output is correct |
51 |
Correct |
164 ms |
163468 KB |
Output is correct |
52 |
Correct |
157 ms |
163492 KB |
Output is correct |
53 |
Correct |
32 ms |
44196 KB |
Output is correct |
54 |
Correct |
63 ms |
77472 KB |
Output is correct |
55 |
Correct |
100 ms |
111680 KB |
Output is correct |
56 |
Correct |
156 ms |
163456 KB |
Output is correct |
57 |
Correct |
159 ms |
163460 KB |
Output is correct |
58 |
Execution timed out |
1092 ms |
18568 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |