#include<bits/stdc++.h>
using namespace std;
const int N = 402, INF = 1e9;
int n, a[N], dp[2][3][N][N], pref[3][N][N];
vector<int> kth_char[3];
string s;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n>>s;
for(int i=0; i<n; ++i) {
if(s[i] == 'R') a[i] = 0;
else if(s[i] == 'G') a[i] = 1;
else a[i] = 2;
}
for(int c=0; c<3; ++c) {
kth_char[c].push_back(-1);
}
for(int i=0; i<n; ++i) {
kth_char[a[i]].push_back(i);
}
for(int c=0; c<3; ++c) {
for(int i=0; i<(int)kth_char[c].size(); ++i) {
int cnt = i;
for(int j=0; j<n; ++j) {
if(j!=0) pref[c][i][j] = pref[c][i][j-1];
if(cnt>0 && a[j]==c) {
pref[c][i][j] ++;
cnt --;
}
}
}
}
for(int c=0; c<3; ++c) {
for(int i=0; i<=n; ++i) {
for(int j=0; j<=n; ++j) {
dp[1][c][i][j] = INF;
}
}
dp[1][c][0][0] = 0;
}
for(int it=0; it<n; ++it) {
for(int c=0; c<3; ++c) {
for(int i=0; i<(int)kth_char[0].size(); ++i) {
for(int j=0; j<(int)kth_char[1].size(); ++j) {
int k = it+1 - i - j;
dp[it%2][c][i][j] = INF;
if(k < 0 || k >= (int)kth_char[2].size()) continue;
if(c==0 && i>0) {
int pos = kth_char[c][i];
dp[it%2][c][i][j] = min(dp[(it+1)%2][1][i-1][j], dp[(it+1)%2][2][i-1][j]) + pos + 1 - (pref[0][i][pos] + pref[1][j][pos] + pref[2][k][pos]) ;
}
if(c==1 && j>0) {
int pos = kth_char[c][j];
dp[it%2][c][i][j] = min(dp[(it+1)%2][0][i][j-1], dp[(it+1)%2][2][i][j-1]) + pos + 1 - (pref[0][i][pos] + pref[1][j][pos] + pref[2][k][pos]) ;
}
if(c==2 && k>0) {
int pos = kth_char[c][k];
dp[it%2][c][i][j] = min(dp[(it+1)%2][0][i][j], dp[(it+1)%2][1][i][j]) + pos + 1 - (pref[0][i][pos] + pref[1][j][pos] + pref[2][k][pos]) ;
}
}
}
}
}
int ans = INF;
for(int c=0; c<3; ++c) {
ans = min(ans, dp[(n-1)%2][c][kth_char[0].size()-1][kth_char[1].size()-1]);
}
if(ans < INF) cout<<ans;
else cout<<-1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
5 ms |
896 KB |
Output is correct |
19 |
Correct |
5 ms |
896 KB |
Output is correct |
20 |
Correct |
6 ms |
896 KB |
Output is correct |
21 |
Correct |
5 ms |
896 KB |
Output is correct |
22 |
Correct |
5 ms |
896 KB |
Output is correct |
23 |
Correct |
5 ms |
896 KB |
Output is correct |
24 |
Correct |
5 ms |
896 KB |
Output is correct |
25 |
Correct |
5 ms |
896 KB |
Output is correct |
26 |
Correct |
5 ms |
896 KB |
Output is correct |
27 |
Correct |
5 ms |
896 KB |
Output is correct |
28 |
Correct |
5 ms |
896 KB |
Output is correct |
29 |
Correct |
5 ms |
896 KB |
Output is correct |
30 |
Correct |
5 ms |
896 KB |
Output is correct |
31 |
Correct |
5 ms |
896 KB |
Output is correct |
32 |
Correct |
5 ms |
896 KB |
Output is correct |
33 |
Correct |
5 ms |
896 KB |
Output is correct |
34 |
Correct |
5 ms |
896 KB |
Output is correct |
35 |
Correct |
5 ms |
896 KB |
Output is correct |
36 |
Correct |
5 ms |
896 KB |
Output is correct |
37 |
Correct |
5 ms |
768 KB |
Output is correct |
38 |
Correct |
5 ms |
768 KB |
Output is correct |
39 |
Correct |
5 ms |
896 KB |
Output is correct |
40 |
Correct |
5 ms |
768 KB |
Output is correct |
41 |
Correct |
5 ms |
896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
55 ms |
3840 KB |
Output is correct |
3 |
Correct |
55 ms |
3840 KB |
Output is correct |
4 |
Correct |
53 ms |
3840 KB |
Output is correct |
5 |
Correct |
54 ms |
3840 KB |
Output is correct |
6 |
Correct |
57 ms |
3960 KB |
Output is correct |
7 |
Correct |
57 ms |
3960 KB |
Output is correct |
8 |
Correct |
55 ms |
3916 KB |
Output is correct |
9 |
Correct |
56 ms |
3840 KB |
Output is correct |
10 |
Correct |
56 ms |
3964 KB |
Output is correct |
11 |
Correct |
54 ms |
3840 KB |
Output is correct |
12 |
Correct |
13 ms |
2176 KB |
Output is correct |
13 |
Correct |
22 ms |
2816 KB |
Output is correct |
14 |
Correct |
35 ms |
3328 KB |
Output is correct |
15 |
Correct |
58 ms |
3840 KB |
Output is correct |
16 |
Correct |
55 ms |
3840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
5 ms |
896 KB |
Output is correct |
19 |
Correct |
5 ms |
896 KB |
Output is correct |
20 |
Correct |
6 ms |
896 KB |
Output is correct |
21 |
Correct |
5 ms |
896 KB |
Output is correct |
22 |
Correct |
5 ms |
896 KB |
Output is correct |
23 |
Correct |
5 ms |
896 KB |
Output is correct |
24 |
Correct |
5 ms |
896 KB |
Output is correct |
25 |
Correct |
5 ms |
896 KB |
Output is correct |
26 |
Correct |
5 ms |
896 KB |
Output is correct |
27 |
Correct |
5 ms |
896 KB |
Output is correct |
28 |
Correct |
5 ms |
896 KB |
Output is correct |
29 |
Correct |
5 ms |
896 KB |
Output is correct |
30 |
Correct |
5 ms |
896 KB |
Output is correct |
31 |
Correct |
5 ms |
896 KB |
Output is correct |
32 |
Correct |
5 ms |
896 KB |
Output is correct |
33 |
Correct |
5 ms |
896 KB |
Output is correct |
34 |
Correct |
5 ms |
896 KB |
Output is correct |
35 |
Correct |
5 ms |
896 KB |
Output is correct |
36 |
Correct |
5 ms |
896 KB |
Output is correct |
37 |
Correct |
5 ms |
768 KB |
Output is correct |
38 |
Correct |
5 ms |
768 KB |
Output is correct |
39 |
Correct |
5 ms |
896 KB |
Output is correct |
40 |
Correct |
5 ms |
768 KB |
Output is correct |
41 |
Correct |
5 ms |
896 KB |
Output is correct |
42 |
Correct |
4 ms |
384 KB |
Output is correct |
43 |
Correct |
55 ms |
3840 KB |
Output is correct |
44 |
Correct |
55 ms |
3840 KB |
Output is correct |
45 |
Correct |
53 ms |
3840 KB |
Output is correct |
46 |
Correct |
54 ms |
3840 KB |
Output is correct |
47 |
Correct |
57 ms |
3960 KB |
Output is correct |
48 |
Correct |
57 ms |
3960 KB |
Output is correct |
49 |
Correct |
55 ms |
3916 KB |
Output is correct |
50 |
Correct |
56 ms |
3840 KB |
Output is correct |
51 |
Correct |
56 ms |
3964 KB |
Output is correct |
52 |
Correct |
54 ms |
3840 KB |
Output is correct |
53 |
Correct |
13 ms |
2176 KB |
Output is correct |
54 |
Correct |
22 ms |
2816 KB |
Output is correct |
55 |
Correct |
35 ms |
3328 KB |
Output is correct |
56 |
Correct |
58 ms |
3840 KB |
Output is correct |
57 |
Correct |
55 ms |
3840 KB |
Output is correct |
58 |
Correct |
48 ms |
3456 KB |
Output is correct |
59 |
Correct |
54 ms |
3584 KB |
Output is correct |
60 |
Correct |
50 ms |
3704 KB |
Output is correct |
61 |
Correct |
51 ms |
3584 KB |
Output is correct |
62 |
Correct |
53 ms |
3960 KB |
Output is correct |
63 |
Correct |
56 ms |
3840 KB |
Output is correct |
64 |
Correct |
60 ms |
3712 KB |
Output is correct |
65 |
Correct |
66 ms |
3704 KB |
Output is correct |
66 |
Correct |
55 ms |
3576 KB |
Output is correct |
67 |
Correct |
58 ms |
3456 KB |
Output is correct |
68 |
Correct |
51 ms |
3584 KB |
Output is correct |
69 |
Correct |
49 ms |
3584 KB |
Output is correct |
70 |
Correct |
52 ms |
3584 KB |
Output is correct |
71 |
Correct |
51 ms |
3584 KB |
Output is correct |
72 |
Correct |
27 ms |
4352 KB |
Output is correct |
73 |
Correct |
9 ms |
2944 KB |
Output is correct |