#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 405, inf = 1e9;
int n;
char s[maxn];
int c[maxn];
int dp[maxn][maxn][maxn][3];
int num[maxn][3];
vector<int> p[3];
int cost(int x, int y, int z, int C) {
int pos;
if(C == 0) pos = p[C][x];
if(C == 1) pos = p[C][y];
if(C == 2) pos = p[C][z];
int ret = min(x, num[pos][0]) + min(y, num[pos][1]) + min(z, num[pos][2]);
ret = pos - ret;
return ret;
}
int main() {
scanf("%d%s", &n, s);
vector<int> cnt(3, 0);
for(int i = 0;i < n;i++) {
if(s[i] == 'R') c[i] = 0;
if(s[i] == 'G') c[i] = 1;
if(s[i] == 'Y') c[i] = 2;
}
for(int i = 0;i < n;i++) {
cnt[c[i]]++, p[c[i]].pb(i);
for(int j = 0;j < 3;j++) num[i][j] = cnt[j];
}
for(int i = 0;i <= cnt[0];i++)
for(int j = 0;j <= cnt[1];j++)
for(int k = 0;k <= cnt[2];k++)
for(int l = 0;l < 3;l++)
dp[i][j][k][l] = inf;
for(int i = 0;i < 3;i++)
dp[0][0][0][i] = 0;
for(int i = 0;i <= cnt[0];i++)
for(int j = 0;j <= cnt[1];j++)
for(int k = 0;k <= cnt[2];k++)
for(int a = 0;a < 3;a++) {
if(dp[i][j][k][a] == inf) continue;
if(a != 0 && i < cnt[0])
dp[i + 1][j][k][0] = min(dp[i + 1][j][k][0], dp[i][j][k][a] + cost(i, j, k, 0));
if(a != 1 && j < cnt[1])
dp[i][j + 1][k][1] = min(dp[i][j + 1][k][1], dp[i][j][k][a] + cost(i, j, k, 1));
if(a != 2 && k < cnt[2])
dp[i][j][k + 1][2] = min(dp[i][j][k + 1][2], dp[i][j][k][a] + cost(i, j, k, 2));
}
int ans = inf;
for(int i = 0;i < 3;i++)
ans = min(dp[cnt[0]][cnt[1]][cnt[2]][i], ans);
if(ans == inf) ans = -1;
printf("%d\n", ans);
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:34:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | scanf("%d%s", &n, s);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
440 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
440 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
18 |
Correct |
2 ms |
2260 KB |
Output is correct |
19 |
Correct |
1 ms |
1876 KB |
Output is correct |
20 |
Correct |
1 ms |
2284 KB |
Output is correct |
21 |
Correct |
1 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4052 KB |
Output is correct |
27 |
Correct |
2 ms |
3028 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2232 KB |
Output is correct |
30 |
Correct |
1 ms |
2228 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
2 ms |
2260 KB |
Output is correct |
33 |
Correct |
2 ms |
3768 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
2 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
1848 KB |
Output is correct |
37 |
Correct |
1 ms |
1620 KB |
Output is correct |
38 |
Correct |
2 ms |
2132 KB |
Output is correct |
39 |
Correct |
2 ms |
2260 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
63 ms |
162964 KB |
Output is correct |
3 |
Correct |
63 ms |
162152 KB |
Output is correct |
4 |
Correct |
66 ms |
162948 KB |
Output is correct |
5 |
Correct |
66 ms |
162972 KB |
Output is correct |
6 |
Correct |
60 ms |
162968 KB |
Output is correct |
7 |
Correct |
62 ms |
162156 KB |
Output is correct |
8 |
Correct |
60 ms |
162132 KB |
Output is correct |
9 |
Correct |
61 ms |
161276 KB |
Output is correct |
10 |
Correct |
69 ms |
162920 KB |
Output is correct |
11 |
Correct |
61 ms |
162904 KB |
Output is correct |
12 |
Correct |
18 ms |
44060 KB |
Output is correct |
13 |
Correct |
30 ms |
77040 KB |
Output is correct |
14 |
Correct |
43 ms |
111308 KB |
Output is correct |
15 |
Correct |
62 ms |
162892 KB |
Output is correct |
16 |
Correct |
61 ms |
162892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
440 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
18 |
Correct |
2 ms |
2260 KB |
Output is correct |
19 |
Correct |
1 ms |
1876 KB |
Output is correct |
20 |
Correct |
1 ms |
2284 KB |
Output is correct |
21 |
Correct |
1 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4052 KB |
Output is correct |
27 |
Correct |
2 ms |
3028 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2232 KB |
Output is correct |
30 |
Correct |
1 ms |
2228 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
2 ms |
2260 KB |
Output is correct |
33 |
Correct |
2 ms |
3768 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
2 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
1848 KB |
Output is correct |
37 |
Correct |
1 ms |
1620 KB |
Output is correct |
38 |
Correct |
2 ms |
2132 KB |
Output is correct |
39 |
Correct |
2 ms |
2260 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
63 ms |
162964 KB |
Output is correct |
44 |
Correct |
63 ms |
162152 KB |
Output is correct |
45 |
Correct |
66 ms |
162948 KB |
Output is correct |
46 |
Correct |
66 ms |
162972 KB |
Output is correct |
47 |
Correct |
60 ms |
162968 KB |
Output is correct |
48 |
Correct |
62 ms |
162156 KB |
Output is correct |
49 |
Correct |
60 ms |
162132 KB |
Output is correct |
50 |
Correct |
61 ms |
161276 KB |
Output is correct |
51 |
Correct |
69 ms |
162920 KB |
Output is correct |
52 |
Correct |
61 ms |
162904 KB |
Output is correct |
53 |
Correct |
18 ms |
44060 KB |
Output is correct |
54 |
Correct |
30 ms |
77040 KB |
Output is correct |
55 |
Correct |
43 ms |
111308 KB |
Output is correct |
56 |
Correct |
62 ms |
162892 KB |
Output is correct |
57 |
Correct |
61 ms |
162892 KB |
Output is correct |
58 |
Correct |
79 ms |
76208 KB |
Output is correct |
59 |
Correct |
84 ms |
96560 KB |
Output is correct |
60 |
Correct |
80 ms |
87720 KB |
Output is correct |
61 |
Correct |
88 ms |
84860 KB |
Output is correct |
62 |
Correct |
64 ms |
160552 KB |
Output is correct |
63 |
Correct |
71 ms |
160164 KB |
Output is correct |
64 |
Correct |
73 ms |
150092 KB |
Output is correct |
65 |
Correct |
75 ms |
138552 KB |
Output is correct |
66 |
Correct |
77 ms |
86088 KB |
Output is correct |
67 |
Correct |
84 ms |
82788 KB |
Output is correct |
68 |
Correct |
84 ms |
88692 KB |
Output is correct |
69 |
Correct |
79 ms |
85460 KB |
Output is correct |
70 |
Correct |
82 ms |
92552 KB |
Output is correct |
71 |
Correct |
77 ms |
83920 KB |
Output is correct |
72 |
Correct |
22 ms |
41556 KB |
Output is correct |
73 |
Correct |
2 ms |
2772 KB |
Output is correct |