#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int dp[3][401][401][401],pos[3][401],cnt[3];
int cost[3][3][401][401];
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 c1=0;c1<3;c1++){
for(int c2=0;c2<3;c2++){
if(c1 == c2)continue;
for(int i=0;i<cnt[c1];i++){
for(int j=0;j<cnt[c2];j++){
cost[c1][c2][i][j] = pos[c2][j] > pos[c1][i];
if(j)cost[c1][c2][i][j]+=cost[c1][c2][i][j-1];
}
}
}
}
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 tot = 0;
for(int c3=0;c3<3;c3++){
if(p[c3])tot+=cost[c2][c3][p[c2]][p[c3]-1];
}
if(c2 == 0)chmin(dp[0][i+1][j+1][k],dp[c1][i][j][k] + tot);
if(c2 == 1)chmin(dp[1][i+1][j][k+1],dp[c1][i][j][k] + tot);
if(c2 == 2)chmin(dp[2][i+1][j][k],dp[c1][i][j][k] + tot);
}
}
}
}
}
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';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
816 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 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 |
1100 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1200 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
816 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 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 |
1100 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1200 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
1 ms |
972 KB |
Output is correct |
18 |
Correct |
4 ms |
10060 KB |
Output is correct |
19 |
Correct |
5 ms |
10060 KB |
Output is correct |
20 |
Correct |
4 ms |
10060 KB |
Output is correct |
21 |
Correct |
6 ms |
10060 KB |
Output is correct |
22 |
Correct |
4 ms |
10060 KB |
Output is correct |
23 |
Correct |
5 ms |
10028 KB |
Output is correct |
24 |
Correct |
4 ms |
10060 KB |
Output is correct |
25 |
Correct |
5 ms |
10052 KB |
Output is correct |
26 |
Correct |
4 ms |
10060 KB |
Output is correct |
27 |
Correct |
5 ms |
10068 KB |
Output is correct |
28 |
Correct |
5 ms |
10060 KB |
Output is correct |
29 |
Correct |
7 ms |
10060 KB |
Output is correct |
30 |
Correct |
5 ms |
10060 KB |
Output is correct |
31 |
Correct |
4 ms |
10060 KB |
Output is correct |
32 |
Correct |
5 ms |
10060 KB |
Output is correct |
33 |
Correct |
4 ms |
9676 KB |
Output is correct |
34 |
Correct |
5 ms |
9676 KB |
Output is correct |
35 |
Correct |
6 ms |
9420 KB |
Output is correct |
36 |
Correct |
4 ms |
9676 KB |
Output is correct |
37 |
Correct |
4 ms |
9164 KB |
Output is correct |
38 |
Correct |
4 ms |
10060 KB |
Output is correct |
39 |
Correct |
4 ms |
10060 KB |
Output is correct |
40 |
Correct |
5 ms |
9548 KB |
Output is correct |
41 |
Correct |
4 ms |
10060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
200 ms |
384052 KB |
Output is correct |
3 |
Correct |
191 ms |
382284 KB |
Output is correct |
4 |
Correct |
191 ms |
384080 KB |
Output is correct |
5 |
Correct |
210 ms |
384080 KB |
Output is correct |
6 |
Correct |
190 ms |
384084 KB |
Output is correct |
7 |
Correct |
189 ms |
382168 KB |
Output is correct |
8 |
Correct |
196 ms |
382196 KB |
Output is correct |
9 |
Correct |
189 ms |
380316 KB |
Output is correct |
10 |
Correct |
193 ms |
384024 KB |
Output is correct |
11 |
Correct |
193 ms |
384068 KB |
Output is correct |
12 |
Correct |
49 ms |
104024 KB |
Output is correct |
13 |
Correct |
85 ms |
182056 KB |
Output is correct |
14 |
Correct |
127 ms |
262740 KB |
Output is correct |
15 |
Correct |
196 ms |
384084 KB |
Output is correct |
16 |
Correct |
189 ms |
384020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
816 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 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 |
1100 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1200 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
1 ms |
972 KB |
Output is correct |
18 |
Correct |
4 ms |
10060 KB |
Output is correct |
19 |
Correct |
5 ms |
10060 KB |
Output is correct |
20 |
Correct |
4 ms |
10060 KB |
Output is correct |
21 |
Correct |
6 ms |
10060 KB |
Output is correct |
22 |
Correct |
4 ms |
10060 KB |
Output is correct |
23 |
Correct |
5 ms |
10028 KB |
Output is correct |
24 |
Correct |
4 ms |
10060 KB |
Output is correct |
25 |
Correct |
5 ms |
10052 KB |
Output is correct |
26 |
Correct |
4 ms |
10060 KB |
Output is correct |
27 |
Correct |
5 ms |
10068 KB |
Output is correct |
28 |
Correct |
5 ms |
10060 KB |
Output is correct |
29 |
Correct |
7 ms |
10060 KB |
Output is correct |
30 |
Correct |
5 ms |
10060 KB |
Output is correct |
31 |
Correct |
4 ms |
10060 KB |
Output is correct |
32 |
Correct |
5 ms |
10060 KB |
Output is correct |
33 |
Correct |
4 ms |
9676 KB |
Output is correct |
34 |
Correct |
5 ms |
9676 KB |
Output is correct |
35 |
Correct |
6 ms |
9420 KB |
Output is correct |
36 |
Correct |
4 ms |
9676 KB |
Output is correct |
37 |
Correct |
4 ms |
9164 KB |
Output is correct |
38 |
Correct |
4 ms |
10060 KB |
Output is correct |
39 |
Correct |
4 ms |
10060 KB |
Output is correct |
40 |
Correct |
5 ms |
9548 KB |
Output is correct |
41 |
Correct |
4 ms |
10060 KB |
Output is correct |
42 |
Correct |
0 ms |
332 KB |
Output is correct |
43 |
Correct |
200 ms |
384052 KB |
Output is correct |
44 |
Correct |
191 ms |
382284 KB |
Output is correct |
45 |
Correct |
191 ms |
384080 KB |
Output is correct |
46 |
Correct |
210 ms |
384080 KB |
Output is correct |
47 |
Correct |
190 ms |
384084 KB |
Output is correct |
48 |
Correct |
189 ms |
382168 KB |
Output is correct |
49 |
Correct |
196 ms |
382196 KB |
Output is correct |
50 |
Correct |
189 ms |
380316 KB |
Output is correct |
51 |
Correct |
193 ms |
384024 KB |
Output is correct |
52 |
Correct |
193 ms |
384068 KB |
Output is correct |
53 |
Correct |
49 ms |
104024 KB |
Output is correct |
54 |
Correct |
85 ms |
182056 KB |
Output is correct |
55 |
Correct |
127 ms |
262740 KB |
Output is correct |
56 |
Correct |
196 ms |
384084 KB |
Output is correct |
57 |
Correct |
189 ms |
384020 KB |
Output is correct |
58 |
Correct |
272 ms |
384728 KB |
Output is correct |
59 |
Correct |
241 ms |
384708 KB |
Output is correct |
60 |
Correct |
243 ms |
380920 KB |
Output is correct |
61 |
Correct |
253 ms |
382756 KB |
Output is correct |
62 |
Correct |
190 ms |
384708 KB |
Output is correct |
63 |
Correct |
198 ms |
384736 KB |
Output is correct |
64 |
Correct |
215 ms |
384776 KB |
Output is correct |
65 |
Correct |
211 ms |
382756 KB |
Output is correct |
66 |
Correct |
242 ms |
379060 KB |
Output is correct |
67 |
Correct |
250 ms |
384708 KB |
Output is correct |
68 |
Correct |
253 ms |
382792 KB |
Output is correct |
69 |
Correct |
255 ms |
384676 KB |
Output is correct |
70 |
Correct |
244 ms |
384752 KB |
Output is correct |
71 |
Correct |
244 ms |
382836 KB |
Output is correct |
72 |
Correct |
222 ms |
382880 KB |
Output is correct |
73 |
Correct |
193 ms |
384728 KB |
Output is correct |