#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int key(char c){
if (c == 'R') return 0;
if (c == 'G') return 1;
if (c == 'Y') return 2;
}
const int INF = 1e9 + 9;
int main(){
cin.tie(0)->sync_with_stdio(0);
int n; cin >> n;
string s; cin >> s;
vector<int> pos[3];
for (int i = 0; i < n; i++){
pos[key(s[i])].push_back(i);
}
int R = pos[0].size(), G = pos[1].size(), Y = pos[2].size();
auto val = [&](vector<int> &a, int x, int k){
int ret = lower_bound(a.begin(), a.end(), x) - a.begin();
return max(0, ret - k);
};
vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(n + 1, vector<int>(3, INF)));
dp[0][0][0] = dp[0][0][1] = dp[0][0][2] = 0;
for (int t = 1; t <= n; t++){
vector<vector<vector<int>>> tmp(n + 1, vector<vector<int>>(n + 1, vector<int>(3, INF)));
for (int i = 1; i <= t && i <= R; i++){
for (int j = 0; i + j <= t && j <= G; j++){
int k = t - i - j;
if (k <= Y)
tmp[i][j][0] = min(tmp[i][j][0], min(dp[i - 1][j][1], dp[i - 1][j][2]) + val(pos[1], pos[0][i - 1], j) + val(pos[2], pos[0][i - 1], k));
}
}
for (int i = 0; i <= t && i <= R; i++){
for (int j = 1; i + j <= t && j <= G; j++){
int k = t - i - j;
if (k <= Y)
tmp[i][j][1] = min(tmp[i][j][1], min(dp[i][j - 1][0], dp[i][j - 1][2]) + val(pos[0], pos[1][j - 1], i) + val(pos[2], pos[1][j - 1], k));
}
}
for (int i = 0; i <= t && i <= R; i++){
for (int j = 0; i + j <= t && j <= G; j++){
int k = t - i - j;
if (0 < k && k <= Y)
tmp[i][j][2] = min(tmp[i][j][2], min(dp[i][j][0], dp[i][j][1]) + val(pos[0], pos[2][k - 1], i) + val(pos[1], pos[2][k - 1], j));
}
}
swap(dp, tmp);
}
int ans = min({dp[R][G][0], dp[R][G][1], dp[R][G][2]});
cout << (ans == INF? -1: ans) << '\n';
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int key(char)':
joi2019_ho_t3.cpp:10:1: warning: control reaches end of non-void function [-Wreturn-type]
10 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
324 KB |
Output is correct |
18 |
Correct |
10 ms |
708 KB |
Output is correct |
19 |
Correct |
11 ms |
724 KB |
Output is correct |
20 |
Correct |
10 ms |
736 KB |
Output is correct |
21 |
Correct |
11 ms |
724 KB |
Output is correct |
22 |
Correct |
10 ms |
724 KB |
Output is correct |
23 |
Correct |
11 ms |
724 KB |
Output is correct |
24 |
Correct |
11 ms |
740 KB |
Output is correct |
25 |
Correct |
10 ms |
704 KB |
Output is correct |
26 |
Correct |
10 ms |
704 KB |
Output is correct |
27 |
Correct |
10 ms |
724 KB |
Output is correct |
28 |
Correct |
11 ms |
732 KB |
Output is correct |
29 |
Correct |
10 ms |
724 KB |
Output is correct |
30 |
Correct |
11 ms |
740 KB |
Output is correct |
31 |
Correct |
10 ms |
724 KB |
Output is correct |
32 |
Correct |
10 ms |
736 KB |
Output is correct |
33 |
Correct |
10 ms |
724 KB |
Output is correct |
34 |
Correct |
10 ms |
720 KB |
Output is correct |
35 |
Correct |
9 ms |
712 KB |
Output is correct |
36 |
Correct |
10 ms |
720 KB |
Output is correct |
37 |
Correct |
9 ms |
596 KB |
Output is correct |
38 |
Correct |
10 ms |
740 KB |
Output is correct |
39 |
Correct |
10 ms |
736 KB |
Output is correct |
40 |
Correct |
10 ms |
724 KB |
Output is correct |
41 |
Correct |
10 ms |
732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1081 ms |
17864 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
324 KB |
Output is correct |
18 |
Correct |
10 ms |
708 KB |
Output is correct |
19 |
Correct |
11 ms |
724 KB |
Output is correct |
20 |
Correct |
10 ms |
736 KB |
Output is correct |
21 |
Correct |
11 ms |
724 KB |
Output is correct |
22 |
Correct |
10 ms |
724 KB |
Output is correct |
23 |
Correct |
11 ms |
724 KB |
Output is correct |
24 |
Correct |
11 ms |
740 KB |
Output is correct |
25 |
Correct |
10 ms |
704 KB |
Output is correct |
26 |
Correct |
10 ms |
704 KB |
Output is correct |
27 |
Correct |
10 ms |
724 KB |
Output is correct |
28 |
Correct |
11 ms |
732 KB |
Output is correct |
29 |
Correct |
10 ms |
724 KB |
Output is correct |
30 |
Correct |
11 ms |
740 KB |
Output is correct |
31 |
Correct |
10 ms |
724 KB |
Output is correct |
32 |
Correct |
10 ms |
736 KB |
Output is correct |
33 |
Correct |
10 ms |
724 KB |
Output is correct |
34 |
Correct |
10 ms |
720 KB |
Output is correct |
35 |
Correct |
9 ms |
712 KB |
Output is correct |
36 |
Correct |
10 ms |
720 KB |
Output is correct |
37 |
Correct |
9 ms |
596 KB |
Output is correct |
38 |
Correct |
10 ms |
740 KB |
Output is correct |
39 |
Correct |
10 ms |
736 KB |
Output is correct |
40 |
Correct |
10 ms |
724 KB |
Output is correct |
41 |
Correct |
10 ms |
732 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Execution timed out |
1081 ms |
17864 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |