#include <bits/stdc++.h>
using namespace std;
template<class X, class Y>
inline void mini(X &x, Y y) {
if (x > y) x = y;
}
const int N = 404;
int sum[N][3], dp[N][N][N][3];
vector<int> pos[3];
string s;
int n;
inline int get(int c, int l, int r) {
return sum[r][c] - sum[l - 1][c];
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
map<char, int> mp;
mp['R'] = 0;
mp['G'] = 1;
mp['Y'] = 2;
cin >> n >> s;
for (int i = 1; i <= n; i++) {
int val = mp[s[i - 1]];
for (int j = 0; j < 3; j++)
sum[i][j] = sum[i - 1][j];
sum[i][val]++;
pos[val].emplace_back(i);
}
memset(dp, 63, sizeof dp);
int INF = dp[0][0][0][0];
vector<int> num;
for (int s: {0, 1, 2}) {
dp[0][0][0][s] = 0;
num.emplace_back((int)pos[s].size());
}
vector<int> a(3, 0), p(3, 0);
for (a[0] = 0; a[0] <= num[0]; a[0]++) {
for (a[1] = 0; a[1] <= num[1]; a[1]++) {
for (a[2] = 0; a[2] <= num[2]; a[2]++) {
for (int lst = 0; lst < 3; lst++) {
int cur = dp[a[0]][a[1]][a[2]][lst];
if (cur == INF) continue;
for (int d = 0; d < 3; d++) {
if (d == lst || a[d] == num[d]) continue;
int now = cur, where = pos[d][a[d]];
for (int i = 0; i < 3; i++)
if (d != i) now += max(sum[where][i] - a[i], 0);
a[d]++;
mini(dp[a[0]][a[1]][a[2]][d], now);
a[d]--;
}
}
if (a[2] < num[2]) p[2] = pos[2][a[2]];
}
if (a[1] < num[1]) p[1] = pos[1][a[1]];
}
if (a[0] < num[0]) p[0] = pos[0][a[0]];
}
int ans = INF;
for (int i = 0; i < 3; i++)
mini(ans, dp[num[0]][num[1]][num[2]][i]);
if (ans == INF) cout << -1;
else cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
774632 KB |
Output is correct |
2 |
Correct |
82 ms |
774488 KB |
Output is correct |
3 |
Correct |
87 ms |
774484 KB |
Output is correct |
4 |
Correct |
82 ms |
774596 KB |
Output is correct |
5 |
Correct |
85 ms |
774532 KB |
Output is correct |
6 |
Correct |
82 ms |
774440 KB |
Output is correct |
7 |
Correct |
83 ms |
774480 KB |
Output is correct |
8 |
Correct |
82 ms |
774500 KB |
Output is correct |
9 |
Correct |
83 ms |
774544 KB |
Output is correct |
10 |
Correct |
81 ms |
774480 KB |
Output is correct |
11 |
Correct |
82 ms |
774740 KB |
Output is correct |
12 |
Correct |
82 ms |
774484 KB |
Output is correct |
13 |
Correct |
84 ms |
774576 KB |
Output is correct |
14 |
Correct |
83 ms |
774432 KB |
Output is correct |
15 |
Correct |
82 ms |
774560 KB |
Output is correct |
16 |
Correct |
82 ms |
774480 KB |
Output is correct |
17 |
Correct |
83 ms |
774480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
774632 KB |
Output is correct |
2 |
Correct |
82 ms |
774488 KB |
Output is correct |
3 |
Correct |
87 ms |
774484 KB |
Output is correct |
4 |
Correct |
82 ms |
774596 KB |
Output is correct |
5 |
Correct |
85 ms |
774532 KB |
Output is correct |
6 |
Correct |
82 ms |
774440 KB |
Output is correct |
7 |
Correct |
83 ms |
774480 KB |
Output is correct |
8 |
Correct |
82 ms |
774500 KB |
Output is correct |
9 |
Correct |
83 ms |
774544 KB |
Output is correct |
10 |
Correct |
81 ms |
774480 KB |
Output is correct |
11 |
Correct |
82 ms |
774740 KB |
Output is correct |
12 |
Correct |
82 ms |
774484 KB |
Output is correct |
13 |
Correct |
84 ms |
774576 KB |
Output is correct |
14 |
Correct |
83 ms |
774432 KB |
Output is correct |
15 |
Correct |
82 ms |
774560 KB |
Output is correct |
16 |
Correct |
82 ms |
774480 KB |
Output is correct |
17 |
Correct |
83 ms |
774480 KB |
Output is correct |
18 |
Correct |
85 ms |
774484 KB |
Output is correct |
19 |
Correct |
86 ms |
774568 KB |
Output is correct |
20 |
Correct |
83 ms |
774680 KB |
Output is correct |
21 |
Correct |
87 ms |
774480 KB |
Output is correct |
22 |
Correct |
83 ms |
774484 KB |
Output is correct |
23 |
Correct |
84 ms |
774620 KB |
Output is correct |
24 |
Correct |
81 ms |
774444 KB |
Output is correct |
25 |
Correct |
84 ms |
774484 KB |
Output is correct |
26 |
Correct |
82 ms |
774580 KB |
Output is correct |
27 |
Correct |
84 ms |
774568 KB |
Output is correct |
28 |
Correct |
82 ms |
774512 KB |
Output is correct |
29 |
Correct |
83 ms |
774488 KB |
Output is correct |
30 |
Correct |
85 ms |
774448 KB |
Output is correct |
31 |
Correct |
82 ms |
774560 KB |
Output is correct |
32 |
Correct |
83 ms |
774544 KB |
Output is correct |
33 |
Correct |
85 ms |
774636 KB |
Output is correct |
34 |
Correct |
83 ms |
774480 KB |
Output is correct |
35 |
Correct |
83 ms |
774480 KB |
Output is correct |
36 |
Correct |
84 ms |
774652 KB |
Output is correct |
37 |
Correct |
83 ms |
774480 KB |
Output is correct |
38 |
Correct |
84 ms |
774480 KB |
Output is correct |
39 |
Correct |
84 ms |
774644 KB |
Output is correct |
40 |
Correct |
85 ms |
774480 KB |
Output is correct |
41 |
Correct |
81 ms |
774624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
774660 KB |
Output is correct |
2 |
Correct |
84 ms |
774544 KB |
Output is correct |
3 |
Correct |
83 ms |
774660 KB |
Output is correct |
4 |
Correct |
83 ms |
774480 KB |
Output is correct |
5 |
Correct |
83 ms |
774484 KB |
Output is correct |
6 |
Correct |
83 ms |
774608 KB |
Output is correct |
7 |
Correct |
83 ms |
774480 KB |
Output is correct |
8 |
Correct |
86 ms |
774744 KB |
Output is correct |
9 |
Correct |
83 ms |
774480 KB |
Output is correct |
10 |
Correct |
85 ms |
774540 KB |
Output is correct |
11 |
Correct |
84 ms |
774664 KB |
Output is correct |
12 |
Correct |
84 ms |
774480 KB |
Output is correct |
13 |
Correct |
85 ms |
774664 KB |
Output is correct |
14 |
Correct |
82 ms |
774620 KB |
Output is correct |
15 |
Correct |
83 ms |
774480 KB |
Output is correct |
16 |
Correct |
85 ms |
774616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
774632 KB |
Output is correct |
2 |
Correct |
82 ms |
774488 KB |
Output is correct |
3 |
Correct |
87 ms |
774484 KB |
Output is correct |
4 |
Correct |
82 ms |
774596 KB |
Output is correct |
5 |
Correct |
85 ms |
774532 KB |
Output is correct |
6 |
Correct |
82 ms |
774440 KB |
Output is correct |
7 |
Correct |
83 ms |
774480 KB |
Output is correct |
8 |
Correct |
82 ms |
774500 KB |
Output is correct |
9 |
Correct |
83 ms |
774544 KB |
Output is correct |
10 |
Correct |
81 ms |
774480 KB |
Output is correct |
11 |
Correct |
82 ms |
774740 KB |
Output is correct |
12 |
Correct |
82 ms |
774484 KB |
Output is correct |
13 |
Correct |
84 ms |
774576 KB |
Output is correct |
14 |
Correct |
83 ms |
774432 KB |
Output is correct |
15 |
Correct |
82 ms |
774560 KB |
Output is correct |
16 |
Correct |
82 ms |
774480 KB |
Output is correct |
17 |
Correct |
83 ms |
774480 KB |
Output is correct |
18 |
Correct |
85 ms |
774484 KB |
Output is correct |
19 |
Correct |
86 ms |
774568 KB |
Output is correct |
20 |
Correct |
83 ms |
774680 KB |
Output is correct |
21 |
Correct |
87 ms |
774480 KB |
Output is correct |
22 |
Correct |
83 ms |
774484 KB |
Output is correct |
23 |
Correct |
84 ms |
774620 KB |
Output is correct |
24 |
Correct |
81 ms |
774444 KB |
Output is correct |
25 |
Correct |
84 ms |
774484 KB |
Output is correct |
26 |
Correct |
82 ms |
774580 KB |
Output is correct |
27 |
Correct |
84 ms |
774568 KB |
Output is correct |
28 |
Correct |
82 ms |
774512 KB |
Output is correct |
29 |
Correct |
83 ms |
774488 KB |
Output is correct |
30 |
Correct |
85 ms |
774448 KB |
Output is correct |
31 |
Correct |
82 ms |
774560 KB |
Output is correct |
32 |
Correct |
83 ms |
774544 KB |
Output is correct |
33 |
Correct |
85 ms |
774636 KB |
Output is correct |
34 |
Correct |
83 ms |
774480 KB |
Output is correct |
35 |
Correct |
83 ms |
774480 KB |
Output is correct |
36 |
Correct |
84 ms |
774652 KB |
Output is correct |
37 |
Correct |
83 ms |
774480 KB |
Output is correct |
38 |
Correct |
84 ms |
774480 KB |
Output is correct |
39 |
Correct |
84 ms |
774644 KB |
Output is correct |
40 |
Correct |
85 ms |
774480 KB |
Output is correct |
41 |
Correct |
81 ms |
774624 KB |
Output is correct |
42 |
Correct |
83 ms |
774660 KB |
Output is correct |
43 |
Correct |
84 ms |
774544 KB |
Output is correct |
44 |
Correct |
83 ms |
774660 KB |
Output is correct |
45 |
Correct |
83 ms |
774480 KB |
Output is correct |
46 |
Correct |
83 ms |
774484 KB |
Output is correct |
47 |
Correct |
83 ms |
774608 KB |
Output is correct |
48 |
Correct |
83 ms |
774480 KB |
Output is correct |
49 |
Correct |
86 ms |
774744 KB |
Output is correct |
50 |
Correct |
83 ms |
774480 KB |
Output is correct |
51 |
Correct |
85 ms |
774540 KB |
Output is correct |
52 |
Correct |
84 ms |
774664 KB |
Output is correct |
53 |
Correct |
84 ms |
774480 KB |
Output is correct |
54 |
Correct |
85 ms |
774664 KB |
Output is correct |
55 |
Correct |
82 ms |
774620 KB |
Output is correct |
56 |
Correct |
83 ms |
774480 KB |
Output is correct |
57 |
Correct |
85 ms |
774616 KB |
Output is correct |
58 |
Correct |
133 ms |
774484 KB |
Output is correct |
59 |
Correct |
139 ms |
774480 KB |
Output is correct |
60 |
Correct |
140 ms |
774488 KB |
Output is correct |
61 |
Correct |
138 ms |
774684 KB |
Output is correct |
62 |
Correct |
89 ms |
774484 KB |
Output is correct |
63 |
Correct |
91 ms |
774484 KB |
Output is correct |
64 |
Correct |
101 ms |
774484 KB |
Output is correct |
65 |
Correct |
114 ms |
774564 KB |
Output is correct |
66 |
Correct |
136 ms |
774468 KB |
Output is correct |
67 |
Correct |
133 ms |
774476 KB |
Output is correct |
68 |
Correct |
138 ms |
774676 KB |
Output is correct |
69 |
Correct |
137 ms |
774484 KB |
Output is correct |
70 |
Correct |
138 ms |
774544 KB |
Output is correct |
71 |
Correct |
134 ms |
774484 KB |
Output is correct |
72 |
Correct |
88 ms |
774484 KB |
Output is correct |
73 |
Correct |
84 ms |
774644 KB |
Output is correct |