#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], dp[405][405][405][3];
char S[405];
vector<int> vec[3];
int cst(int a, int x, int b, int y) {
int tar = vec[a][x - 1];
return max(0ll, y - (lower_bound(vec[b].begin(), vec[b].end(), tar) - vec[b].begin()));
}
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 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:38:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
38 | main() {
| ^~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:47:10: warning: array subscript has type 'char' [-Wchar-subscripts]
47 | cnt[S[i]]++;
| ~~~^
joi2019_ho_t3.cpp:48:10: warning: array subscript has type 'char' [-Wchar-subscripts]
48 | vec[S[i]].pb(i);
| ~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 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 |
464 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 |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 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 |
464 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 |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
3 ms |
2380 KB |
Output is correct |
19 |
Correct |
3 ms |
2004 KB |
Output is correct |
20 |
Correct |
3 ms |
2388 KB |
Output is correct |
21 |
Correct |
3 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
1748 KB |
Output is correct |
23 |
Correct |
3 ms |
2260 KB |
Output is correct |
24 |
Correct |
2 ms |
1748 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4176 KB |
Output is correct |
27 |
Correct |
3 ms |
3156 KB |
Output is correct |
28 |
Correct |
2 ms |
2392 KB |
Output is correct |
29 |
Correct |
2 ms |
2388 KB |
Output is correct |
30 |
Correct |
2 ms |
2388 KB |
Output is correct |
31 |
Correct |
2 ms |
2132 KB |
Output is correct |
32 |
Correct |
3 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 |
3 ms |
2772 KB |
Output is correct |
36 |
Correct |
2 ms |
2012 KB |
Output is correct |
37 |
Correct |
2 ms |
1748 KB |
Output is correct |
38 |
Correct |
3 ms |
2260 KB |
Output is correct |
39 |
Correct |
3 ms |
2388 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
2260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
91 ms |
163996 KB |
Output is correct |
3 |
Correct |
74 ms |
163312 KB |
Output is correct |
4 |
Correct |
73 ms |
164072 KB |
Output is correct |
5 |
Correct |
76 ms |
163996 KB |
Output is correct |
6 |
Correct |
93 ms |
163976 KB |
Output is correct |
7 |
Correct |
70 ms |
163192 KB |
Output is correct |
8 |
Correct |
69 ms |
163140 KB |
Output is correct |
9 |
Correct |
71 ms |
162396 KB |
Output is correct |
10 |
Correct |
77 ms |
164044 KB |
Output is correct |
11 |
Correct |
80 ms |
164088 KB |
Output is correct |
12 |
Correct |
20 ms |
44352 KB |
Output is correct |
13 |
Correct |
35 ms |
77772 KB |
Output is correct |
14 |
Correct |
49 ms |
112064 KB |
Output is correct |
15 |
Correct |
86 ms |
163992 KB |
Output is correct |
16 |
Correct |
96 ms |
164040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 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 |
464 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 |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
3 ms |
2380 KB |
Output is correct |
19 |
Correct |
3 ms |
2004 KB |
Output is correct |
20 |
Correct |
3 ms |
2388 KB |
Output is correct |
21 |
Correct |
3 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
1748 KB |
Output is correct |
23 |
Correct |
3 ms |
2260 KB |
Output is correct |
24 |
Correct |
2 ms |
1748 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4176 KB |
Output is correct |
27 |
Correct |
3 ms |
3156 KB |
Output is correct |
28 |
Correct |
2 ms |
2392 KB |
Output is correct |
29 |
Correct |
2 ms |
2388 KB |
Output is correct |
30 |
Correct |
2 ms |
2388 KB |
Output is correct |
31 |
Correct |
2 ms |
2132 KB |
Output is correct |
32 |
Correct |
3 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 |
3 ms |
2772 KB |
Output is correct |
36 |
Correct |
2 ms |
2012 KB |
Output is correct |
37 |
Correct |
2 ms |
1748 KB |
Output is correct |
38 |
Correct |
3 ms |
2260 KB |
Output is correct |
39 |
Correct |
3 ms |
2388 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
2260 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
91 ms |
163996 KB |
Output is correct |
44 |
Correct |
74 ms |
163312 KB |
Output is correct |
45 |
Correct |
73 ms |
164072 KB |
Output is correct |
46 |
Correct |
76 ms |
163996 KB |
Output is correct |
47 |
Correct |
93 ms |
163976 KB |
Output is correct |
48 |
Correct |
70 ms |
163192 KB |
Output is correct |
49 |
Correct |
69 ms |
163140 KB |
Output is correct |
50 |
Correct |
71 ms |
162396 KB |
Output is correct |
51 |
Correct |
77 ms |
164044 KB |
Output is correct |
52 |
Correct |
80 ms |
164088 KB |
Output is correct |
53 |
Correct |
20 ms |
44352 KB |
Output is correct |
54 |
Correct |
35 ms |
77772 KB |
Output is correct |
55 |
Correct |
49 ms |
112064 KB |
Output is correct |
56 |
Correct |
86 ms |
163992 KB |
Output is correct |
57 |
Correct |
96 ms |
164040 KB |
Output is correct |
58 |
Execution timed out |
715 ms |
120596 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |