#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);
int n;
cin >> n;
map<char, int> s;
s['R'] = 0;
s['G'] = 1;
s['Y'] = 2;
vector<vector<int>> t(3, vector (1, 0)), sum(3, vector (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<vector<int>>>> dp(sum[0][n] + 1, vector (sum[1][n] + 1, vector (sum[2][n] + 1, vector (3, (int)1e9))));
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 = 1e9;
REP (i, 3) {
odp = min(odp, dp[sum[0][n]][sum[1][n]][sum[2][n]][i]);
}
if (odp == (int)1e9) {
cout << "-1" << endl;
return 0;
}
cout << odp << endl;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
316 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
320 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 |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
316 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
320 KB |
Output is correct |
18 |
Correct |
1 ms |
852 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
448 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
836 KB |
Output is correct |
29 |
Correct |
1 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
852 KB |
Output is correct |
31 |
Correct |
1 ms |
832 KB |
Output is correct |
32 |
Correct |
1 ms |
852 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
724 KB |
Output is correct |
36 |
Correct |
1 ms |
724 KB |
Output is correct |
37 |
Correct |
1 ms |
724 KB |
Output is correct |
38 |
Correct |
1 ms |
724 KB |
Output is correct |
39 |
Correct |
1 ms |
852 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
240 KB |
Output is correct |
2 |
Correct |
6 ms |
3776 KB |
Output is correct |
3 |
Correct |
5 ms |
3796 KB |
Output is correct |
4 |
Correct |
5 ms |
3796 KB |
Output is correct |
5 |
Correct |
5 ms |
3796 KB |
Output is correct |
6 |
Correct |
5 ms |
3796 KB |
Output is correct |
7 |
Correct |
6 ms |
3776 KB |
Output is correct |
8 |
Correct |
5 ms |
3780 KB |
Output is correct |
9 |
Correct |
5 ms |
3796 KB |
Output is correct |
10 |
Correct |
5 ms |
3796 KB |
Output is correct |
11 |
Correct |
5 ms |
3796 KB |
Output is correct |
12 |
Correct |
2 ms |
1236 KB |
Output is correct |
13 |
Correct |
3 ms |
1876 KB |
Output is correct |
14 |
Correct |
4 ms |
2644 KB |
Output is correct |
15 |
Correct |
6 ms |
3796 KB |
Output is correct |
16 |
Correct |
6 ms |
3796 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 |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
316 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
320 KB |
Output is correct |
18 |
Correct |
1 ms |
852 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
448 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
836 KB |
Output is correct |
29 |
Correct |
1 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
852 KB |
Output is correct |
31 |
Correct |
1 ms |
832 KB |
Output is correct |
32 |
Correct |
1 ms |
852 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
724 KB |
Output is correct |
36 |
Correct |
1 ms |
724 KB |
Output is correct |
37 |
Correct |
1 ms |
724 KB |
Output is correct |
38 |
Correct |
1 ms |
724 KB |
Output is correct |
39 |
Correct |
1 ms |
852 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
240 KB |
Output is correct |
43 |
Correct |
6 ms |
3776 KB |
Output is correct |
44 |
Correct |
5 ms |
3796 KB |
Output is correct |
45 |
Correct |
5 ms |
3796 KB |
Output is correct |
46 |
Correct |
5 ms |
3796 KB |
Output is correct |
47 |
Correct |
5 ms |
3796 KB |
Output is correct |
48 |
Correct |
6 ms |
3776 KB |
Output is correct |
49 |
Correct |
5 ms |
3780 KB |
Output is correct |
50 |
Correct |
5 ms |
3796 KB |
Output is correct |
51 |
Correct |
5 ms |
3796 KB |
Output is correct |
52 |
Correct |
5 ms |
3796 KB |
Output is correct |
53 |
Correct |
2 ms |
1236 KB |
Output is correct |
54 |
Correct |
3 ms |
1876 KB |
Output is correct |
55 |
Correct |
4 ms |
2644 KB |
Output is correct |
56 |
Correct |
6 ms |
3796 KB |
Output is correct |
57 |
Correct |
6 ms |
3796 KB |
Output is correct |
58 |
Correct |
184 ms |
133164 KB |
Output is correct |
59 |
Correct |
185 ms |
133228 KB |
Output is correct |
60 |
Correct |
180 ms |
132284 KB |
Output is correct |
61 |
Correct |
178 ms |
133776 KB |
Output is correct |
62 |
Correct |
13 ms |
12372 KB |
Output is correct |
63 |
Correct |
29 ms |
20692 KB |
Output is correct |
64 |
Correct |
98 ms |
66576 KB |
Output is correct |
65 |
Correct |
149 ms |
95544 KB |
Output is correct |
66 |
Correct |
173 ms |
131668 KB |
Output is correct |
67 |
Correct |
193 ms |
131920 KB |
Output is correct |
68 |
Correct |
178 ms |
133680 KB |
Output is correct |
69 |
Correct |
184 ms |
134604 KB |
Output is correct |
70 |
Correct |
184 ms |
134132 KB |
Output is correct |
71 |
Correct |
202 ms |
131028 KB |
Output is correct |
72 |
Correct |
36 ms |
34132 KB |
Output is correct |
73 |
Correct |
10 ms |
10548 KB |
Output is correct |