#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int dp[3][401][401][401],pos[3][401],cnt[3];
void chmin(int &x,int y){
x = min(x,y);
}
int main()
{
int n;
string s;
cin>>n>>s;
vector<int>c;
for(char x:s){
if(x == 'R')c.push_back(0);
if(x == 'G')c.push_back(1);
if(x == 'Y')c.push_back(2);
}
for(int i=0;i<=n;i++)pos[0][i] = pos[1][i] = pos[2][i] = -1;
for(int i=0;i<n;i++)pos[c[i]][cnt[c[i]]++] = i;
/*
for(int i=0;i<3;i++){
for(int j=0;j<cnt[i];j++)cout<<pos[i][j]<<" ";
cout<<'\n';
}*/
for(int i=0;i<=n;i++)
for(int j=0;j<=i;j++)
for(int k=0;k+j<=i;k++)
dp[0][i][j][k] = dp[1][i][j][k] = dp[2][i][j][k] = INF;
dp[0][0][0][0] = dp[1][0][0][0] = dp[2][0][0][0] = 0;
array<int,3>p;
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
for(int k=0;k+j<=i;k++){
p = {j,k,i-j-k}; //number of rgy
for(int c1=0;c1<3;c1++){ //previous color
if(dp[c1][i][j][k] == INF)continue;
for(int c2=0;c2<3;c2++){ //current color
if(c1 == c2 || pos[c2][p[c2]] == -1)continue;
int cost = 0;
for(int c3=0;c3<3;c3++){
if(c3 == c2)continue;
for(int ii=0;ii<p[c3];ii++)cost+=pos[c3][ii] > pos[c2][p[c2]];
}
if(c2 == 0)chmin(dp[0][i+1][j+1][k],dp[c1][i][j][k] + cost);
if(c2 == 1)chmin(dp[1][i+1][j][k+1],dp[c1][i][j][k] + cost);
if(c2 == 2)chmin(dp[2][i+1][j][k],dp[c1][i][j][k] + cost);
}
}
}
}
}
int mn = INF;
for(int i=0;i<3;i++)mn = min(mn,dp[i][n][cnt[0]][cnt[1]]);
if(mn == INF)mn = -1;
cout<<mn<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
0 ms |
300 KB |
Output is correct |
4 |
Correct |
0 ms |
716 KB |
Output is correct |
5 |
Correct |
1 ms |
1056 KB |
Output is correct |
6 |
Correct |
1 ms |
1092 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1068 KB |
Output is correct |
12 |
Correct |
1 ms |
1068 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1072 KB |
Output is correct |
17 |
Correct |
1 ms |
1064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
0 ms |
300 KB |
Output is correct |
4 |
Correct |
0 ms |
716 KB |
Output is correct |
5 |
Correct |
1 ms |
1056 KB |
Output is correct |
6 |
Correct |
1 ms |
1092 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1068 KB |
Output is correct |
12 |
Correct |
1 ms |
1068 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1072 KB |
Output is correct |
17 |
Correct |
1 ms |
1064 KB |
Output is correct |
18 |
Correct |
6 ms |
9804 KB |
Output is correct |
19 |
Correct |
6 ms |
9804 KB |
Output is correct |
20 |
Correct |
6 ms |
9820 KB |
Output is correct |
21 |
Correct |
6 ms |
9804 KB |
Output is correct |
22 |
Correct |
5 ms |
9804 KB |
Output is correct |
23 |
Correct |
6 ms |
9752 KB |
Output is correct |
24 |
Correct |
5 ms |
9804 KB |
Output is correct |
25 |
Correct |
4 ms |
9804 KB |
Output is correct |
26 |
Correct |
4 ms |
9768 KB |
Output is correct |
27 |
Correct |
5 ms |
9868 KB |
Output is correct |
28 |
Correct |
5 ms |
9804 KB |
Output is correct |
29 |
Correct |
6 ms |
9804 KB |
Output is correct |
30 |
Correct |
5 ms |
9804 KB |
Output is correct |
31 |
Correct |
6 ms |
9804 KB |
Output is correct |
32 |
Correct |
6 ms |
9932 KB |
Output is correct |
33 |
Correct |
4 ms |
9576 KB |
Output is correct |
34 |
Correct |
5 ms |
9548 KB |
Output is correct |
35 |
Correct |
7 ms |
9272 KB |
Output is correct |
36 |
Correct |
6 ms |
9600 KB |
Output is correct |
37 |
Correct |
5 ms |
8908 KB |
Output is correct |
38 |
Correct |
5 ms |
9768 KB |
Output is correct |
39 |
Correct |
6 ms |
9864 KB |
Output is correct |
40 |
Correct |
4 ms |
9548 KB |
Output is correct |
41 |
Correct |
5 ms |
9804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
217 ms |
383372 KB |
Output is correct |
3 |
Correct |
204 ms |
381564 KB |
Output is correct |
4 |
Correct |
209 ms |
383376 KB |
Output is correct |
5 |
Correct |
216 ms |
383368 KB |
Output is correct |
6 |
Correct |
202 ms |
383364 KB |
Output is correct |
7 |
Correct |
208 ms |
381672 KB |
Output is correct |
8 |
Correct |
201 ms |
381564 KB |
Output is correct |
9 |
Correct |
204 ms |
379584 KB |
Output is correct |
10 |
Correct |
222 ms |
383596 KB |
Output is correct |
11 |
Correct |
197 ms |
383352 KB |
Output is correct |
12 |
Correct |
50 ms |
103756 KB |
Output is correct |
13 |
Correct |
106 ms |
181576 KB |
Output is correct |
14 |
Correct |
151 ms |
262096 KB |
Output is correct |
15 |
Correct |
198 ms |
383388 KB |
Output is correct |
16 |
Correct |
198 ms |
383508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
0 ms |
300 KB |
Output is correct |
4 |
Correct |
0 ms |
716 KB |
Output is correct |
5 |
Correct |
1 ms |
1056 KB |
Output is correct |
6 |
Correct |
1 ms |
1092 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1068 KB |
Output is correct |
12 |
Correct |
1 ms |
1068 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1072 KB |
Output is correct |
17 |
Correct |
1 ms |
1064 KB |
Output is correct |
18 |
Correct |
6 ms |
9804 KB |
Output is correct |
19 |
Correct |
6 ms |
9804 KB |
Output is correct |
20 |
Correct |
6 ms |
9820 KB |
Output is correct |
21 |
Correct |
6 ms |
9804 KB |
Output is correct |
22 |
Correct |
5 ms |
9804 KB |
Output is correct |
23 |
Correct |
6 ms |
9752 KB |
Output is correct |
24 |
Correct |
5 ms |
9804 KB |
Output is correct |
25 |
Correct |
4 ms |
9804 KB |
Output is correct |
26 |
Correct |
4 ms |
9768 KB |
Output is correct |
27 |
Correct |
5 ms |
9868 KB |
Output is correct |
28 |
Correct |
5 ms |
9804 KB |
Output is correct |
29 |
Correct |
6 ms |
9804 KB |
Output is correct |
30 |
Correct |
5 ms |
9804 KB |
Output is correct |
31 |
Correct |
6 ms |
9804 KB |
Output is correct |
32 |
Correct |
6 ms |
9932 KB |
Output is correct |
33 |
Correct |
4 ms |
9576 KB |
Output is correct |
34 |
Correct |
5 ms |
9548 KB |
Output is correct |
35 |
Correct |
7 ms |
9272 KB |
Output is correct |
36 |
Correct |
6 ms |
9600 KB |
Output is correct |
37 |
Correct |
5 ms |
8908 KB |
Output is correct |
38 |
Correct |
5 ms |
9768 KB |
Output is correct |
39 |
Correct |
6 ms |
9864 KB |
Output is correct |
40 |
Correct |
4 ms |
9548 KB |
Output is correct |
41 |
Correct |
5 ms |
9804 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
217 ms |
383372 KB |
Output is correct |
44 |
Correct |
204 ms |
381564 KB |
Output is correct |
45 |
Correct |
209 ms |
383376 KB |
Output is correct |
46 |
Correct |
216 ms |
383368 KB |
Output is correct |
47 |
Correct |
202 ms |
383364 KB |
Output is correct |
48 |
Correct |
208 ms |
381672 KB |
Output is correct |
49 |
Correct |
201 ms |
381564 KB |
Output is correct |
50 |
Correct |
204 ms |
379584 KB |
Output is correct |
51 |
Correct |
222 ms |
383596 KB |
Output is correct |
52 |
Correct |
197 ms |
383352 KB |
Output is correct |
53 |
Correct |
50 ms |
103756 KB |
Output is correct |
54 |
Correct |
106 ms |
181576 KB |
Output is correct |
55 |
Correct |
151 ms |
262096 KB |
Output is correct |
56 |
Correct |
198 ms |
383388 KB |
Output is correct |
57 |
Correct |
198 ms |
383508 KB |
Output is correct |
58 |
Execution timed out |
1116 ms |
383428 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |