#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update>
/*
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,bmi2,fma,popcnt")
*/
#ifdef lisie_bimbi
#define debug(x) cout << #x << " : " << x << endl;
#else
#define debug(x) ;
#define endl '\n'
#endif
//#define int long long
#define inf 1000000000
#define double long double
typedef long long ll;
struct yeei{
int r;
int g;
int y;
};
signed main(){
#ifdef lisie_bimbi
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#endif
int n;
cin >> n;
string s;
cin >> s;
int r = 0, g = 0, y = 0;
vector<int> vr, vg, vy;
for(int i = 0; i < n; i++){
if(s[i] == 'R'){
r++;
vr.push_back(i);
}else if(s[i] == 'G'){
g++;
vg.push_back(i);
}else{
y++;
vy.push_back(i);
}
}
vector<vector<vector<yeei>>> dp(r + 1, vector<vector<yeei>>(g + 1, vector<yeei>(y + 1, {inf, inf, inf})));
dp[0][0][0] = {0, 0, 0};
for(int r1 = 0; r1 <= r; r1++){
for(int g1 = 0; g1 <= g; g1++){
for(int y1 = 0; y1 <= y; y1++){
if((r1 == 0) && (g1 == 0) && (y1 == 0)){
continue;
}
if(r1 != 0){
int x = vr[r1 - 1];
int cnt = g1 + y1;
cnt -= min(g1, (int)(std::lower_bound(vg.begin(), vg.end(), x) - vg.begin()));
cnt -= min(y1, (int)(std::lower_bound(vy.begin(), vy.end(), x) - vy.begin()));
dp[r1][g1][y1].r = min(dp[r1][g1][y1].r, dp[r1 - 1][g1][y1].g + cnt);
dp[r1][g1][y1].r = min(dp[r1][g1][y1].r, dp[r1 - 1][g1][y1].y + cnt);
}
if(g1 != 0){
int x = vg[g1 - 1];
int cnt = r1 + y1;
cnt -= min(r1, (int)(std::lower_bound(vr.begin(), vr.end(), x) - vr.begin()));
cnt -= min(y1, (int)(std::lower_bound(vy.begin(), vy.end(), x) - vy.begin()));
dp[r1][g1][y1].g = min(dp[r1][g1][y1].g, dp[r1][g1 - 1][y1].r + cnt);
dp[r1][g1][y1].g = min(dp[r1][g1][y1].g, dp[r1][g1 - 1][y1].y + cnt);
}
if(y1 != 0){
int x = vy[y1 - 1];
int cnt = g1 + r1;
cnt -= min(g1, (int)(std::lower_bound(vg.begin(), vg.end(), x) - vg.begin()));
cnt -= min(r1, (int)(std::lower_bound(vr.begin(), vr.end(), x) - vr.begin()));
dp[r1][g1][y1].y = min(dp[r1][g1][y1].y, dp[r1][g1][y1 - 1].g + cnt);
dp[r1][g1][y1].y = min(dp[r1][g1][y1].y, dp[r1][g1][y1 - 1].r + cnt);
}
}
}
}
int x = min(dp[r][g][y].r, min(dp[r][g][y].g, dp[r][g][y].y));
if(x < inf){
cout << x;
}else{
cout << -1;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
5 ms |
2692 KB |
Output is correct |
3 |
Correct |
3 ms |
2652 KB |
Output is correct |
4 |
Correct |
3 ms |
2652 KB |
Output is correct |
5 |
Correct |
3 ms |
2652 KB |
Output is correct |
6 |
Correct |
4 ms |
2612 KB |
Output is correct |
7 |
Correct |
3 ms |
2652 KB |
Output is correct |
8 |
Correct |
3 ms |
2536 KB |
Output is correct |
9 |
Correct |
3 ms |
2648 KB |
Output is correct |
10 |
Correct |
4 ms |
2652 KB |
Output is correct |
11 |
Correct |
6 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
856 KB |
Output is correct |
13 |
Correct |
2 ms |
1372 KB |
Output is correct |
14 |
Correct |
2 ms |
1884 KB |
Output is correct |
15 |
Correct |
4 ms |
2652 KB |
Output is correct |
16 |
Correct |
4 ms |
2624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
5 ms |
2692 KB |
Output is correct |
44 |
Correct |
3 ms |
2652 KB |
Output is correct |
45 |
Correct |
3 ms |
2652 KB |
Output is correct |
46 |
Correct |
3 ms |
2652 KB |
Output is correct |
47 |
Correct |
4 ms |
2612 KB |
Output is correct |
48 |
Correct |
3 ms |
2652 KB |
Output is correct |
49 |
Correct |
3 ms |
2536 KB |
Output is correct |
50 |
Correct |
3 ms |
2648 KB |
Output is correct |
51 |
Correct |
4 ms |
2652 KB |
Output is correct |
52 |
Correct |
6 ms |
2652 KB |
Output is correct |
53 |
Correct |
1 ms |
856 KB |
Output is correct |
54 |
Correct |
2 ms |
1372 KB |
Output is correct |
55 |
Correct |
2 ms |
1884 KB |
Output is correct |
56 |
Correct |
4 ms |
2652 KB |
Output is correct |
57 |
Correct |
4 ms |
2624 KB |
Output is correct |
58 |
Correct |
143 ms |
29276 KB |
Output is correct |
59 |
Correct |
141 ms |
29532 KB |
Output is correct |
60 |
Correct |
139 ms |
29176 KB |
Output is correct |
61 |
Correct |
147 ms |
29532 KB |
Output is correct |
62 |
Correct |
9 ms |
4440 KB |
Output is correct |
63 |
Correct |
16 ms |
6236 KB |
Output is correct |
64 |
Correct |
80 ms |
15536 KB |
Output is correct |
65 |
Correct |
97 ms |
21336 KB |
Output is correct |
66 |
Correct |
114 ms |
29020 KB |
Output is correct |
67 |
Correct |
198 ms |
29020 KB |
Output is correct |
68 |
Correct |
118 ms |
29272 KB |
Output is correct |
69 |
Correct |
113 ms |
29788 KB |
Output is correct |
70 |
Correct |
121 ms |
29836 KB |
Output is correct |
71 |
Correct |
167 ms |
28760 KB |
Output is correct |
72 |
Correct |
36 ms |
7772 KB |
Output is correct |
73 |
Correct |
8 ms |
2648 KB |
Output is correct |