#include<bits/stdc++.h>
#define ll long long
#define int long long
const int nmax = 4e5 + 5, N = 4e5;
const ll oo = 1e9;
const int lg = 18, M = 4e3;
const int base = 2e5, mod = 1e9 + 7;
#define pii pair<int, int>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' ';cout << endl
using namespace std;
int n;
int cnt[nmax][3];
void ckmin(int &a, int b){
a = min(a, b);
}
vector<int> adj[3];
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("code.inp", "r", stdin);
// freopen("code.out", "w", stdout);
// cin >> n;
string s;
cin >> n;
cin >> s;
s = ' ' + s;
for(int i = 1; i <= n; ++i){
int idx;
if(s[i] == 'R') idx = 0;
else if(s[i] == 'G') idx = 1;
else idx = 2;
adj[idx].push_back(i);
for(int j = 0; j < 3; ++j) cnt[i][j] = cnt[i - 1][j];
cnt[i][idx]++;
}
for(int j = 0; j < 3; ++j) adj[j].push_back(oo);
vector<vector<vector<vector<int>>>> dp(cnt[n][0] + 2, vector<vector<vector<int>>> (cnt[n][1] + 2, vector<vector<int>>(cnt[n][2] + 2, vector<int>(3, oo))));
for(int j = 0; j < 3; ++j) dp[0][0][0][j] = 0;
for(int c1 = 0; c1 <= cnt[n][0];++c1){
for(int c2 = 0; c2 <= cnt[n][1];++c2){
for(int c3 = 0; c3 <= cnt[n][2];++c3){
int i = c1 + c2 + c3;
int cur = 0;
for(int j = 0; j < 3; ++j){
if(j == 0) continue;
int x = adj[0][c1];
int p1 = max(0ll, c2 - (lower_bound(adj[1].begin(), adj[1].end(), x) - adj[1].begin()));
int p2 = max(0ll, c3 - (lower_bound(adj[2].begin(), adj[2].end(), x) - adj[2].begin()));
int cur = x + p1 + p2 - i - 1;
ckmin(dp[c1 + 1][c2][c3][0], dp[c1][c2][c3][j] + cur);
//
}
for(int j = 0; j < 3; ++j){
if(j == 1) continue;
int x = adj[1][c2];
int p1 = max(0ll, c1 - (lower_bound(adj[0].begin(), adj[0].end(), x) - adj[0].begin()));
int p2 = max(0ll, c3 - (lower_bound(adj[2].begin(), adj[2].end(), x) - adj[2].begin()));
int cur = x + p1 + p2 - i - 1;
ckmin(dp[c1][c2 + 1][c3][1], dp[c1][c2][c3][j] + cur);
}
for(int j = 0; j < 3; ++j){
if(j == 2) continue;
int x = adj[2][c3];
// if(c3 <= cnt[i][2]){
//
// cur = x - i - 1;
// ckmin(dp[c1][c2][c3 + 1][2], dp[c1][c2][c3][j] + cur);
// }
// else{
int p1 = max(0ll, c1 - (lower_bound(adj[0].begin(), adj[0].end(), x) - adj[0].begin()));
int p2 = max(0ll, c2 - (lower_bound(adj[1].begin(), adj[1].end(), x) - adj[1].begin()));
int cur = x + p1 + p2 - i - 1;
ckmin(dp[c1][c2][c3 + 1][2], dp[c1][c2][c3][j] + cur);
// }
}
}
}
}
// cout << dp[0][2][3][2]<< ' ';
int ans = oo;
for(int i = 0; i < 3; ++i)ans = min(ans, dp[cnt[n][0]][cnt[n][1]][cnt[n][2]][i]);
if(ans > oo/2) cout << -1;
else cout << ans;
}
/*
25
RYRRRYGYGGYYGYYRGGYGGGRGG
*/
Compilation message
joi2019_ho_t3.cpp:22:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
22 | main(){
| ^~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:50:21: warning: unused variable 'cur' [-Wunused-variable]
50 | int cur = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 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 |
1 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 |
0 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 |
1 ms |
348 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 |
1 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 |
0 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 |
2 ms |
856 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
996 KB |
Output is correct |
25 |
Correct |
0 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
1 ms |
860 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
0 ms |
464 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
8 ms |
6492 KB |
Output is correct |
3 |
Correct |
8 ms |
6492 KB |
Output is correct |
4 |
Correct |
12 ms |
6492 KB |
Output is correct |
5 |
Correct |
8 ms |
6492 KB |
Output is correct |
6 |
Correct |
8 ms |
6364 KB |
Output is correct |
7 |
Correct |
9 ms |
6492 KB |
Output is correct |
8 |
Correct |
9 ms |
6492 KB |
Output is correct |
9 |
Correct |
8 ms |
6316 KB |
Output is correct |
10 |
Correct |
8 ms |
6492 KB |
Output is correct |
11 |
Correct |
8 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
1884 KB |
Output is correct |
13 |
Correct |
4 ms |
3284 KB |
Output is correct |
14 |
Correct |
6 ms |
4444 KB |
Output is correct |
15 |
Correct |
9 ms |
6492 KB |
Output is correct |
16 |
Correct |
9 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 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 |
1 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 |
0 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 |
2 ms |
856 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
996 KB |
Output is correct |
25 |
Correct |
0 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
1 ms |
860 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
0 ms |
464 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
8 ms |
6492 KB |
Output is correct |
44 |
Correct |
8 ms |
6492 KB |
Output is correct |
45 |
Correct |
12 ms |
6492 KB |
Output is correct |
46 |
Correct |
8 ms |
6492 KB |
Output is correct |
47 |
Correct |
8 ms |
6364 KB |
Output is correct |
48 |
Correct |
9 ms |
6492 KB |
Output is correct |
49 |
Correct |
9 ms |
6492 KB |
Output is correct |
50 |
Correct |
8 ms |
6316 KB |
Output is correct |
51 |
Correct |
8 ms |
6492 KB |
Output is correct |
52 |
Correct |
8 ms |
6492 KB |
Output is correct |
53 |
Correct |
2 ms |
1884 KB |
Output is correct |
54 |
Correct |
4 ms |
3284 KB |
Output is correct |
55 |
Correct |
6 ms |
4444 KB |
Output is correct |
56 |
Correct |
9 ms |
6492 KB |
Output is correct |
57 |
Correct |
9 ms |
6492 KB |
Output is correct |
58 |
Correct |
335 ms |
136276 KB |
Output is correct |
59 |
Correct |
348 ms |
136592 KB |
Output is correct |
60 |
Correct |
355 ms |
135252 KB |
Output is correct |
61 |
Correct |
324 ms |
136792 KB |
Output is correct |
62 |
Correct |
29 ms |
14940 KB |
Output is correct |
63 |
Correct |
47 ms |
23592 KB |
Output is correct |
64 |
Correct |
180 ms |
69456 KB |
Output is correct |
65 |
Correct |
257 ms |
98128 KB |
Output is correct |
66 |
Correct |
325 ms |
134760 KB |
Output is correct |
67 |
Correct |
314 ms |
134856 KB |
Output is correct |
68 |
Correct |
337 ms |
136532 KB |
Output is correct |
69 |
Correct |
318 ms |
137556 KB |
Output is correct |
70 |
Correct |
326 ms |
137272 KB |
Output is correct |
71 |
Correct |
343 ms |
134260 KB |
Output is correct |
72 |
Correct |
97 ms |
36004 KB |
Output is correct |
73 |
Correct |
23 ms |
12376 KB |
Output is correct |