#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
int N, cnt[3], tmp[3][405][3], dp[405][405][405][3];
char S[405];
vector<int> vec[3];
int cst(int a, int x, int b, int y) {
return max(0ll, y - tmp[a][x][b]);
}
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> S[i];
if (S[i] == 'R') S[i] = 0;
else if (S[i] == 'G') S[i] = 1;
else S[i] = 2;
cnt[S[i]]++;
vec[S[i]].pb(i);
}
for (int a = 0; a < 3; a++)
for (int x = 1; x <= cnt[a]; x++)
for (int b = 0; b < 3; b++)
if (a != b) tmp[a][x][b] = lower_bound(vec[b].begin(), vec[b].end(), vec[a][x - 1]) - vec[b].begin();
for (int sum = cnt[0] + cnt[1] + cnt[2]; sum >= 0; sum--) {
for (int i = 0; i <= cnt[0]; i++)
for (int j = max(0ll, sum - i - cnt[2]); j <= min(sum - i, cnt[1]); j++) {
int k = sum - i - j;
for (int p = 0; p < 3; p++) {
if (i + j + k == cnt[0] + cnt[1] + cnt[2]) {
dp[i][j][k][p] = 0;
continue;
}
dp[i][j][k][p] = 1e16;
if (i < cnt[0] && p != 0) dp[i][j][k][p] = min(dp[i][j][k][p], dp[i + 1][j][k][0] + cst(0, i + 1, 1, j) + cst(0, i + 1, 2, k));
if (j < cnt[1] && p != 1) dp[i][j][k][p] = min(dp[i][j][k][p], dp[i][j + 1][k][1] + cst(1, j + 1, 0, i) + cst(1, j + 1, 2, k));
if (k < cnt[2] && p != 2) dp[i][j][k][p] = min(dp[i][j][k][p], dp[i][j][k + 1][2] + cst(2, k + 1, 0, i) + cst(2, k + 1, 1, j));
}
}
}
int r = min({dp[0][0][0][0], dp[0][0][0][1], dp[0][0][0][2]});
if (r >= (int)1e15) cout << "-1\n";
else cout << r << '\n';
}
Compilation message
joi2019_ho_t3.cpp:37:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
37 | main() {
| ^~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:46:10: warning: array subscript has type 'char' [-Wchar-subscripts]
46 | cnt[S[i]]++;
| ~~~^
joi2019_ho_t3.cpp:47:10: warning: array subscript has type 'char' [-Wchar-subscripts]
47 | vec[S[i]].pb(i);
| ~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
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 |
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 |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
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 |
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 |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
2388 KB |
Output is correct |
19 |
Correct |
1 ms |
2004 KB |
Output is correct |
20 |
Correct |
1 ms |
2388 KB |
Output is correct |
21 |
Correct |
1 ms |
2516 KB |
Output is correct |
22 |
Correct |
1 ms |
1748 KB |
Output is correct |
23 |
Correct |
2 ms |
2260 KB |
Output is correct |
24 |
Correct |
1 ms |
1748 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
3 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3156 KB |
Output is correct |
28 |
Correct |
2 ms |
2388 KB |
Output is correct |
29 |
Correct |
1 ms |
2388 KB |
Output is correct |
30 |
Correct |
1 ms |
2388 KB |
Output is correct |
31 |
Correct |
1 ms |
2132 KB |
Output is correct |
32 |
Correct |
1 ms |
2516 KB |
Output is correct |
33 |
Correct |
2 ms |
3924 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
1 ms |
2004 KB |
Output is correct |
37 |
Correct |
1 ms |
1748 KB |
Output is correct |
38 |
Correct |
1 ms |
2260 KB |
Output is correct |
39 |
Correct |
2 ms |
2388 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
68 ms |
164032 KB |
Output is correct |
3 |
Correct |
68 ms |
163208 KB |
Output is correct |
4 |
Correct |
64 ms |
164176 KB |
Output is correct |
5 |
Correct |
67 ms |
164044 KB |
Output is correct |
6 |
Correct |
76 ms |
164020 KB |
Output is correct |
7 |
Correct |
73 ms |
163148 KB |
Output is correct |
8 |
Correct |
69 ms |
163252 KB |
Output is correct |
9 |
Correct |
65 ms |
162368 KB |
Output is correct |
10 |
Correct |
68 ms |
164092 KB |
Output is correct |
11 |
Correct |
66 ms |
163984 KB |
Output is correct |
12 |
Correct |
19 ms |
44372 KB |
Output is correct |
13 |
Correct |
34 ms |
77696 KB |
Output is correct |
14 |
Correct |
50 ms |
112092 KB |
Output is correct |
15 |
Correct |
65 ms |
164016 KB |
Output is correct |
16 |
Correct |
66 ms |
163988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
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 |
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 |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
2388 KB |
Output is correct |
19 |
Correct |
1 ms |
2004 KB |
Output is correct |
20 |
Correct |
1 ms |
2388 KB |
Output is correct |
21 |
Correct |
1 ms |
2516 KB |
Output is correct |
22 |
Correct |
1 ms |
1748 KB |
Output is correct |
23 |
Correct |
2 ms |
2260 KB |
Output is correct |
24 |
Correct |
1 ms |
1748 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
3 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3156 KB |
Output is correct |
28 |
Correct |
2 ms |
2388 KB |
Output is correct |
29 |
Correct |
1 ms |
2388 KB |
Output is correct |
30 |
Correct |
1 ms |
2388 KB |
Output is correct |
31 |
Correct |
1 ms |
2132 KB |
Output is correct |
32 |
Correct |
1 ms |
2516 KB |
Output is correct |
33 |
Correct |
2 ms |
3924 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
1 ms |
2004 KB |
Output is correct |
37 |
Correct |
1 ms |
1748 KB |
Output is correct |
38 |
Correct |
1 ms |
2260 KB |
Output is correct |
39 |
Correct |
2 ms |
2388 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2260 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
68 ms |
164032 KB |
Output is correct |
44 |
Correct |
68 ms |
163208 KB |
Output is correct |
45 |
Correct |
64 ms |
164176 KB |
Output is correct |
46 |
Correct |
67 ms |
164044 KB |
Output is correct |
47 |
Correct |
76 ms |
164020 KB |
Output is correct |
48 |
Correct |
73 ms |
163148 KB |
Output is correct |
49 |
Correct |
69 ms |
163252 KB |
Output is correct |
50 |
Correct |
65 ms |
162368 KB |
Output is correct |
51 |
Correct |
68 ms |
164092 KB |
Output is correct |
52 |
Correct |
66 ms |
163984 KB |
Output is correct |
53 |
Correct |
19 ms |
44372 KB |
Output is correct |
54 |
Correct |
34 ms |
77696 KB |
Output is correct |
55 |
Correct |
50 ms |
112092 KB |
Output is correct |
56 |
Correct |
65 ms |
164016 KB |
Output is correct |
57 |
Correct |
66 ms |
163988 KB |
Output is correct |
58 |
Correct |
157 ms |
120652 KB |
Output is correct |
59 |
Correct |
179 ms |
137900 KB |
Output is correct |
60 |
Correct |
150 ms |
130000 KB |
Output is correct |
61 |
Correct |
158 ms |
128108 KB |
Output is correct |
62 |
Correct |
91 ms |
163612 KB |
Output is correct |
63 |
Correct |
103 ms |
164808 KB |
Output is correct |
64 |
Correct |
115 ms |
164464 KB |
Output is correct |
65 |
Correct |
134 ms |
159024 KB |
Output is correct |
66 |
Correct |
168 ms |
128444 KB |
Output is correct |
67 |
Correct |
214 ms |
125568 KB |
Output is correct |
68 |
Correct |
153 ms |
131424 KB |
Output is correct |
69 |
Correct |
155 ms |
128984 KB |
Output is correct |
70 |
Correct |
194 ms |
134860 KB |
Output is correct |
71 |
Correct |
151 ms |
126284 KB |
Output is correct |
72 |
Correct |
45 ms |
49684 KB |
Output is correct |
73 |
Correct |
5 ms |
5460 KB |
Output is correct |