#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 405;
vector<int> red, yellow, green;
int pref[3][N];
int sum(int idx_from, int from, int to) {
if(from > to)
return 0;
return pref[idx_from][to] - pref[idx_from][from];
}
void Solve() {
int n;
cin >> n;
string s;
cin >> s;
int red_cnt = 0, yellow_cnt = 0, green_cnt = 0;
pref[0][0] = 1;
pref[1][0] = 1;
pref[2][0] = 1;
red.push_back(0);
yellow.push_back(0);
green.push_back(0);
for(int i = 0; i < s.size(); i++) {
if(s[i] == 'R') {
red_cnt++;
red.push_back(i + 1);
pref[0][i + 1]++;
}
else if(s[i] == 'Y') {
yellow_cnt++;
yellow.push_back(i + 1);
pref[1][i + 1]++;
}
else {
green_cnt++;
green.push_back(i + 1);
pref[2][i + 1]++;
}
pref[0][i + 1] += pref[0][i];
pref[1][i + 1] += pref[1][i];
pref[2][i + 1] += pref[2][i];
}
int dp[red_cnt + 1][yellow_cnt + 1][green_cnt + 1][3];
for(int i = 0; i <= red_cnt; i++) {
for(int j = 0; j <= yellow_cnt; j++) {
for(int k = 0; k <= green_cnt; k++) {
for(int l = 0; l < 3; l++) {
dp[i][j][k][l] = INT_MAX;
}
}
}
}
for(int l = 0; l < 3; l++) {
dp[0][0][0][l] = 0;
}
for(int i = 0; i <= red_cnt; i++) {
for(int j = 0; j <= yellow_cnt; j++) {
for(int k = 0; k <= green_cnt; k++) {
if(i == 0 && j == 0 && k == 0)
continue;
for(int cur = 0; cur < 3; cur++) {
for(int last = 0; last < 3; last++) {
if(cur == last)
continue;
if(cur == 0) {
if(i == 0)
continue;
dp[i][j][k][cur] = min(dp[i][j][k][cur], dp[i - 1][j][k][last] + sum(1, yellow[j], red[i]) + sum(2, green[k], red[i]));
}
else if(cur == 1) {
if(j == 0)
continue;
dp[i][j][k][cur] = min(dp[i][j][k][cur], dp[i][j - 1][k][last] + sum(0, red[i], yellow[j]) + sum(2, green[k], yellow[j]));
}
else {
if(k == 0)
continue;
dp[i][j][k][cur] = min(dp[i][j][k][cur], dp[i][j][k - 1][last] + sum(0, red[i], green[k]) + sum(1, yellow[j], green[k]));
}
}
}
}
}
}
int ans = min(dp[red_cnt][yellow_cnt][green_cnt][0], min(dp[red_cnt][yellow_cnt][green_cnt][1], dp[red_cnt][yellow_cnt][green_cnt][2]));
if(ans >= 1e7) {
cout << "-1\n";
}
else {
cout << ans << "\n";
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
Solve();
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'void Solve()':
joi2019_ho_t3.cpp:27:19: 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]
27 | for(int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
2 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
2 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
2 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
492 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
492 KB |
Output is correct |
36 |
Correct |
1 ms |
492 KB |
Output is correct |
37 |
Correct |
1 ms |
492 KB |
Output is correct |
38 |
Correct |
1 ms |
492 KB |
Output is correct |
39 |
Correct |
1 ms |
492 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
3 ms |
1260 KB |
Output is correct |
3 |
Correct |
2 ms |
1260 KB |
Output is correct |
4 |
Correct |
3 ms |
1260 KB |
Output is correct |
5 |
Correct |
3 ms |
1260 KB |
Output is correct |
6 |
Correct |
3 ms |
1260 KB |
Output is correct |
7 |
Correct |
3 ms |
1260 KB |
Output is correct |
8 |
Correct |
3 ms |
1260 KB |
Output is correct |
9 |
Correct |
3 ms |
1260 KB |
Output is correct |
10 |
Correct |
3 ms |
1260 KB |
Output is correct |
11 |
Correct |
3 ms |
1260 KB |
Output is correct |
12 |
Correct |
2 ms |
620 KB |
Output is correct |
13 |
Correct |
2 ms |
748 KB |
Output is correct |
14 |
Correct |
2 ms |
1004 KB |
Output is correct |
15 |
Correct |
3 ms |
1260 KB |
Output is correct |
16 |
Correct |
3 ms |
1260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
2 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
2 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
492 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
492 KB |
Output is correct |
36 |
Correct |
1 ms |
492 KB |
Output is correct |
37 |
Correct |
1 ms |
492 KB |
Output is correct |
38 |
Correct |
1 ms |
492 KB |
Output is correct |
39 |
Correct |
1 ms |
492 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
1 ms |
364 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
3 ms |
1260 KB |
Output is correct |
44 |
Correct |
2 ms |
1260 KB |
Output is correct |
45 |
Correct |
3 ms |
1260 KB |
Output is correct |
46 |
Correct |
3 ms |
1260 KB |
Output is correct |
47 |
Correct |
3 ms |
1260 KB |
Output is correct |
48 |
Correct |
3 ms |
1260 KB |
Output is correct |
49 |
Correct |
3 ms |
1260 KB |
Output is correct |
50 |
Correct |
3 ms |
1260 KB |
Output is correct |
51 |
Correct |
3 ms |
1260 KB |
Output is correct |
52 |
Correct |
3 ms |
1260 KB |
Output is correct |
53 |
Correct |
2 ms |
620 KB |
Output is correct |
54 |
Correct |
2 ms |
748 KB |
Output is correct |
55 |
Correct |
2 ms |
1004 KB |
Output is correct |
56 |
Correct |
3 ms |
1260 KB |
Output is correct |
57 |
Correct |
3 ms |
1260 KB |
Output is correct |
58 |
Correct |
120 ms |
56684 KB |
Output is correct |
59 |
Correct |
100 ms |
56684 KB |
Output is correct |
60 |
Correct |
101 ms |
56300 KB |
Output is correct |
61 |
Correct |
102 ms |
56940 KB |
Output is correct |
62 |
Correct |
9 ms |
4972 KB |
Output is correct |
63 |
Correct |
16 ms |
8556 KB |
Output is correct |
64 |
Correct |
50 ms |
28396 KB |
Output is correct |
65 |
Correct |
73 ms |
40428 KB |
Output is correct |
66 |
Correct |
97 ms |
56044 KB |
Output is correct |
67 |
Correct |
95 ms |
56028 KB |
Output is correct |
68 |
Correct |
106 ms |
56812 KB |
Output is correct |
69 |
Correct |
101 ms |
57284 KB |
Output is correct |
70 |
Correct |
117 ms |
57196 KB |
Output is correct |
71 |
Correct |
104 ms |
55788 KB |
Output is correct |
72 |
Correct |
28 ms |
14700 KB |
Output is correct |
73 |
Correct |
8 ms |
3948 KB |
Output is correct |