#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x)
#else
#define debug(...) {}
#endif
int main() {
cin.tie(0)->sync_with_stdio(0);
const int INF = 1e9;
int n;
cin >> n;
map<char, int> s;
s['R'] = 0;
s['G'] = 1;
s['Y'] = 2;
vector<vector<int>> t(3, vector<int> (1, 0)), sum(3, vector<int> (n + 1, 0));
vector<int> v(n);
FOR (i, 1, n) {
char c;
cin >> c;
t[s[c]].emplace_back(i);
sum[s[c]][i] = 1;
}
FOR (i, 1, n) {
REP (j, 3) {
sum[j][i] += sum[j][i - 1];
}
}
vector<vector<vector<array<int, 3>>>> dp(sum[0][n] + 1, vector<vector<array<int, 3>>> (sum[1][n] + 1, vector<array<int, 3>> (sum[2][n] + 1, array<int, 3> {INF, INF, INF})));
REP (i, 3) {
if (sum[i][n] > (n + 1) / 2) {
cout << "-1" << endl;
return 0;
}
dp[0][0][0][i] = 0;
}
FOR (i, 0, sum[0][n]) {
FOR (j, 0, sum[1][n]) {
FOR (k, 0, sum[2][n]) {
if (i) {
dp[i][j][k][0] = min(dp[i - 1][j][k][1], dp[i - 1][j][k][2]) +
max(0, sum[1][t[0][i]] - sum[1][t[1][j]]) +
max(0, sum[2][t[0][i]] - sum[2][t[2][k]]);
}
if (j) {
dp[i][j][k][1] = min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]) +
max(0, sum[0][t[1][j]] - sum[0][t[0][i]]) +
max(0, sum[2][t[1][j]] - sum[2][t[2][k]]);
}
if (k) {
dp[i][j][k][2] = min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]) +
max(0, sum[0][t[2][k]] - sum[0][t[0][i]]) +
max(0, sum[1][t[2][k]] - sum[1][t[1][j]]);
}
}
}
}
int odp = INF;
REP (i, 3) {
odp = min(odp, dp[sum[0][n]][sum[1][n]][sum[2][n]][i]);
}
if (odp == INF) {
cout << "-1" << endl;
return 0;
}
cout << odp << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
316 KB |
Output is correct |
15 |
Correct |
0 ms |
316 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
316 KB |
Output is correct |
15 |
Correct |
0 ms |
316 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
444 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
448 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
2516 KB |
Output is correct |
3 |
Correct |
3 ms |
2516 KB |
Output is correct |
4 |
Correct |
3 ms |
2516 KB |
Output is correct |
5 |
Correct |
3 ms |
2516 KB |
Output is correct |
6 |
Correct |
3 ms |
2516 KB |
Output is correct |
7 |
Correct |
3 ms |
2516 KB |
Output is correct |
8 |
Correct |
3 ms |
2496 KB |
Output is correct |
9 |
Correct |
3 ms |
2516 KB |
Output is correct |
10 |
Correct |
3 ms |
2512 KB |
Output is correct |
11 |
Correct |
3 ms |
2516 KB |
Output is correct |
12 |
Correct |
1 ms |
828 KB |
Output is correct |
13 |
Correct |
2 ms |
1364 KB |
Output is correct |
14 |
Correct |
2 ms |
1852 KB |
Output is correct |
15 |
Correct |
4 ms |
2516 KB |
Output is correct |
16 |
Correct |
4 ms |
2516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
316 KB |
Output is correct |
15 |
Correct |
0 ms |
316 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
444 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
448 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
0 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
3 ms |
2516 KB |
Output is correct |
44 |
Correct |
3 ms |
2516 KB |
Output is correct |
45 |
Correct |
3 ms |
2516 KB |
Output is correct |
46 |
Correct |
3 ms |
2516 KB |
Output is correct |
47 |
Correct |
3 ms |
2516 KB |
Output is correct |
48 |
Correct |
3 ms |
2516 KB |
Output is correct |
49 |
Correct |
3 ms |
2496 KB |
Output is correct |
50 |
Correct |
3 ms |
2516 KB |
Output is correct |
51 |
Correct |
3 ms |
2512 KB |
Output is correct |
52 |
Correct |
3 ms |
2516 KB |
Output is correct |
53 |
Correct |
1 ms |
828 KB |
Output is correct |
54 |
Correct |
2 ms |
1364 KB |
Output is correct |
55 |
Correct |
2 ms |
1852 KB |
Output is correct |
56 |
Correct |
4 ms |
2516 KB |
Output is correct |
57 |
Correct |
4 ms |
2516 KB |
Output is correct |
58 |
Correct |
52 ms |
29256 KB |
Output is correct |
59 |
Correct |
50 ms |
29408 KB |
Output is correct |
60 |
Correct |
48 ms |
28928 KB |
Output is correct |
61 |
Correct |
52 ms |
29396 KB |
Output is correct |
62 |
Correct |
3 ms |
4308 KB |
Output is correct |
63 |
Correct |
25 ms |
6136 KB |
Output is correct |
64 |
Correct |
28 ms |
15424 KB |
Output is correct |
65 |
Correct |
35 ms |
21332 KB |
Output is correct |
66 |
Correct |
50 ms |
29012 KB |
Output is correct |
67 |
Correct |
49 ms |
29124 KB |
Output is correct |
68 |
Correct |
51 ms |
29268 KB |
Output is correct |
69 |
Correct |
51 ms |
29676 KB |
Output is correct |
70 |
Correct |
51 ms |
29708 KB |
Output is correct |
71 |
Correct |
56 ms |
28876 KB |
Output is correct |
72 |
Correct |
4 ms |
7764 KB |
Output is correct |
73 |
Correct |
1 ms |
2516 KB |
Output is correct |