#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
int n;
string s;
const int INF = 1e9 + 9;
int cnt[3];
int pos[3][405];
int pref[3][405];
int a[405];
int dp[3][405][405][405];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s;
for (int i = 0; i < s.size(); i++) {
if (s[i] == 'R') {
a[i + 1] = 0; cnt[0]++;
pos[0][cnt[0]] = i + 1;
} else if (s[i] == 'G') {
a[i + 1] = 1; cnt[1]++;
pos[1][cnt[1]] = i + 1;
} else {
a[i + 1] = 2; cnt[2]++;
pos[2][cnt[2]] = i + 1;
}
}
for (int i = 0; i < 3; i++) {
for (int j = 1; j <= n; j++) {
pref[i][j] = pref[i][j - 1];
if (a[j] == i) pref[i][j]++;
}
}
for (int i = 0; i < 3; i++) {
for (int r = 0; r <= cnt[0]; r++) {
for (int g = 0; g <= cnt[1]; g++) {
for (int y = 0; y <= cnt[2]; y++) {
dp[i][r][g][y] = INF;
}
}
}
}
dp[0][0][0][0] = 0;
dp[1][0][0][0] = 0;
dp[2][0][0][0] = 0;
for (int r = 0; r <= cnt[0]; r++) {
for (int g = 0; g <= cnt[1]; g++) {
for (int y = 0; y <= cnt[2]; y++) {
for (int pre = 0; pre < 3; pre++) {
for (int cur = 0; cur < 3; cur++) {
if (cur == pre) continue;
if ((cur == 0 && r == cnt[0])) continue;
if ((cur == 1 && g == cnt[1])) continue;
if ((cur == 2 && y == cnt[2])) continue;
int ans = 0;
int p1, p2, p3, dif;
if (pre == 0) {
p1 = pos[0][r];
if (cur == 1) {
p2 = pos[1][g + 1];
p3 = pos[2][y];
dif = 2;
} else {
p2 = pos[2][y + 1];
p3 = pos[1][g];
dif = 1;
}
} else if (pre == 1) {
p1 = pos[1][g];
if (cur == 0) {
p2 = pos[0][r + 1];
p3 = pos[2][y];
dif = 2;
} else {
p2 = pos[2][y + 1];
p3 = pos[0][r];
dif = 0;
}
} else {
p1 = pos[2][y];
if (cur == 0) {
p2 = pos[0][r + 1];
p3 = pos[1][g];
dif = 1;
} else {
p2 = pos[1][g + 1];
p3 = pos[0][r];
dif = 0;
}
}
ans += max(0LL, pref[pre][p2] - pref[pre][p1]);
ans += max(0LL, pref[dif][p2] - pref[dif][p3]);
ans += dp[pre][r][g][y];
if (cur == 0) {
dp[cur][r + 1][g][y] = min(dp[cur][r + 1][g][y], ans);
} else if (cur == 1) {
dp[cur][r][g + 1][y] = min(dp[cur][r][g + 1][y], ans);
} else {
dp[cur][r][g][y + 1] = min(dp[cur][r][g][y + 1], ans);
}
}
}
}
}
}
int ans = INF;
for (int i = 0; i < 3; i++) {
ans = min(ans, dp[i][cnt[0]][cnt[1]][cnt[2]]);
}
if (ans == INF) {
cout << -1 << '\n';
} else {
cout << ans << '\n';
}
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:22:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
716 KB |
Output is correct |
7 |
Correct |
1 ms |
716 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
844 KB |
Output is correct |
12 |
Correct |
1 ms |
716 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
716 KB |
Output is correct |
7 |
Correct |
1 ms |
716 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
844 KB |
Output is correct |
12 |
Correct |
1 ms |
716 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
4 ms |
4812 KB |
Output is correct |
19 |
Correct |
3 ms |
3916 KB |
Output is correct |
20 |
Correct |
3 ms |
4812 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
3 ms |
3404 KB |
Output is correct |
23 |
Correct |
3 ms |
4428 KB |
Output is correct |
24 |
Correct |
3 ms |
3276 KB |
Output is correct |
25 |
Correct |
5 ms |
8908 KB |
Output is correct |
26 |
Correct |
5 ms |
9424 KB |
Output is correct |
27 |
Correct |
4 ms |
6732 KB |
Output is correct |
28 |
Correct |
3 ms |
4940 KB |
Output is correct |
29 |
Correct |
3 ms |
4676 KB |
Output is correct |
30 |
Correct |
3 ms |
4684 KB |
Output is correct |
31 |
Correct |
3 ms |
4044 KB |
Output is correct |
32 |
Correct |
3 ms |
4944 KB |
Output is correct |
33 |
Correct |
5 ms |
8736 KB |
Output is correct |
34 |
Correct |
5 ms |
7884 KB |
Output is correct |
35 |
Correct |
4 ms |
5836 KB |
Output is correct |
36 |
Correct |
3 ms |
3916 KB |
Output is correct |
37 |
Correct |
3 ms |
3552 KB |
Output is correct |
38 |
Correct |
3 ms |
4428 KB |
Output is correct |
39 |
Correct |
3 ms |
4812 KB |
Output is correct |
40 |
Correct |
1 ms |
460 KB |
Output is correct |
41 |
Correct |
3 ms |
4812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
170 ms |
385716 KB |
Output is correct |
3 |
Correct |
169 ms |
383924 KB |
Output is correct |
4 |
Correct |
167 ms |
385732 KB |
Output is correct |
5 |
Correct |
167 ms |
385756 KB |
Output is correct |
6 |
Correct |
169 ms |
385732 KB |
Output is correct |
7 |
Correct |
168 ms |
383816 KB |
Output is correct |
8 |
Correct |
170 ms |
383812 KB |
Output is correct |
9 |
Correct |
168 ms |
382020 KB |
Output is correct |
10 |
Correct |
167 ms |
385772 KB |
Output is correct |
11 |
Correct |
167 ms |
385732 KB |
Output is correct |
12 |
Correct |
49 ms |
103956 KB |
Output is correct |
13 |
Correct |
86 ms |
182468 KB |
Output is correct |
14 |
Correct |
118 ms |
263472 KB |
Output is correct |
15 |
Correct |
170 ms |
385756 KB |
Output is correct |
16 |
Correct |
169 ms |
385728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
716 KB |
Output is correct |
7 |
Correct |
1 ms |
716 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
844 KB |
Output is correct |
12 |
Correct |
1 ms |
716 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
4 ms |
4812 KB |
Output is correct |
19 |
Correct |
3 ms |
3916 KB |
Output is correct |
20 |
Correct |
3 ms |
4812 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
3 ms |
3404 KB |
Output is correct |
23 |
Correct |
3 ms |
4428 KB |
Output is correct |
24 |
Correct |
3 ms |
3276 KB |
Output is correct |
25 |
Correct |
5 ms |
8908 KB |
Output is correct |
26 |
Correct |
5 ms |
9424 KB |
Output is correct |
27 |
Correct |
4 ms |
6732 KB |
Output is correct |
28 |
Correct |
3 ms |
4940 KB |
Output is correct |
29 |
Correct |
3 ms |
4676 KB |
Output is correct |
30 |
Correct |
3 ms |
4684 KB |
Output is correct |
31 |
Correct |
3 ms |
4044 KB |
Output is correct |
32 |
Correct |
3 ms |
4944 KB |
Output is correct |
33 |
Correct |
5 ms |
8736 KB |
Output is correct |
34 |
Correct |
5 ms |
7884 KB |
Output is correct |
35 |
Correct |
4 ms |
5836 KB |
Output is correct |
36 |
Correct |
3 ms |
3916 KB |
Output is correct |
37 |
Correct |
3 ms |
3552 KB |
Output is correct |
38 |
Correct |
3 ms |
4428 KB |
Output is correct |
39 |
Correct |
3 ms |
4812 KB |
Output is correct |
40 |
Correct |
1 ms |
460 KB |
Output is correct |
41 |
Correct |
3 ms |
4812 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
170 ms |
385716 KB |
Output is correct |
44 |
Correct |
169 ms |
383924 KB |
Output is correct |
45 |
Correct |
167 ms |
385732 KB |
Output is correct |
46 |
Correct |
167 ms |
385756 KB |
Output is correct |
47 |
Correct |
169 ms |
385732 KB |
Output is correct |
48 |
Correct |
168 ms |
383816 KB |
Output is correct |
49 |
Correct |
170 ms |
383812 KB |
Output is correct |
50 |
Correct |
168 ms |
382020 KB |
Output is correct |
51 |
Correct |
167 ms |
385772 KB |
Output is correct |
52 |
Correct |
167 ms |
385732 KB |
Output is correct |
53 |
Correct |
49 ms |
103956 KB |
Output is correct |
54 |
Correct |
86 ms |
182468 KB |
Output is correct |
55 |
Correct |
118 ms |
263472 KB |
Output is correct |
56 |
Correct |
170 ms |
385756 KB |
Output is correct |
57 |
Correct |
169 ms |
385728 KB |
Output is correct |
58 |
Correct |
193 ms |
152616 KB |
Output is correct |
59 |
Correct |
228 ms |
193608 KB |
Output is correct |
60 |
Correct |
201 ms |
175932 KB |
Output is correct |
61 |
Correct |
200 ms |
169988 KB |
Output is correct |
62 |
Correct |
174 ms |
375912 KB |
Output is correct |
63 |
Correct |
179 ms |
370696 KB |
Output is correct |
64 |
Correct |
202 ms |
323524 KB |
Output is correct |
65 |
Correct |
213 ms |
281612 KB |
Output is correct |
66 |
Correct |
197 ms |
172444 KB |
Output is correct |
67 |
Correct |
203 ms |
165912 KB |
Output is correct |
68 |
Correct |
204 ms |
177600 KB |
Output is correct |
69 |
Correct |
197 ms |
171172 KB |
Output is correct |
70 |
Correct |
212 ms |
185284 KB |
Output is correct |
71 |
Correct |
197 ms |
168120 KB |
Output is correct |
72 |
Correct |
73 ms |
84124 KB |
Output is correct |
73 |
Correct |
11 ms |
5452 KB |
Output is correct |