#include<bits/stdc++.h>
#define f first
#define int long long
#define s second
using namespace std;
const int N=405,mod=1e16;
int t,dp[2][N][N][4],cnt[4],kth[4][N],aft[4][4][N][N],n;
string s;
main(){
// t=1;
cin >> n;
cin>>s;
s='#'+s;
for(int i=1;i<=n;i++){
if(s[i]=='R') s[i]='0';
else if(s[i]=='Y') s[i]='1';
else s[i]='2';
cnt[s[i]-'0']++;
kth[s[i]-'0'][cnt[s[i]-'0']] = i;
}
for(int i=0;i<=2;i++){
for(int j=0;j<=2;j++){
if(i==j) continue;
for(int k=0;k<=cnt[i];k++){
for(int k1=1;k1<=cnt[j];k1++){
aft[i][j][k][k1] = aft[i][j][k][k1-1] + (kth[j][k1] > kth[i][k]);
}
}
}
}
for(int i=0;i<=n;i++){
for(int j=0;j<=cnt[0];j++){
for(int k=0;k<=cnt[1];k++){
dp[0][j][k][0]=dp[0][j][k][1]=dp[0][j][k][2]=mod;
}} }
dp[0][0][0][0]=dp[0][0][0][1]=dp[0][0][0][2]=0;
for(int i=1;i<=n;i++){
for(int j=0;j<=cnt[0];j++){
for(int k=0;k<=cnt[1];k++){
if(i-k-j>cnt[2]) continue;
dp[1][j][k][0]=dp[1][j][k][1]=dp[1][j][k][2]=mod;
if(j) dp[1][j][k][0] =min(mod, min(dp[0][j-1][k][1],dp[0][j-1][k][2]) + kth[0][j] + aft[0][1][j][k] + aft[0][2][j][i-k-j] - i);
if(k) dp[1][j][k][1] =min(mod, min(dp[0][j][k-1][2],dp[0][j][k-1][0]) + kth[1][k] + aft[1][0][k][j] + aft[1][2][k][i-k-j] - i);
if(i-j-k) dp[1][j][k][2] = min(mod,min(dp[0][j][k][1],dp[0][j][k][0]) + kth[2][i-j-k] + aft[2][0][i-j-k][j] + aft[2][1][i-j-k][k] - i);
}
}
for(int j=0;j<=cnt[0];j++){
for(int k=0;k<=cnt[1];k++){
dp[0][j][k][0]=dp[1][j][k][0];
dp[0][j][k][2]=dp[1][j][k][2];
dp[0][j][k][1]=dp[1][j][k][1];
}}
}
int c = min(dp[0][cnt[0]][cnt[1]][0],min(dp[0][cnt[0]][cnt[1]][1],dp[0][cnt[0]][cnt[1]][2]));
if(c==mod)cout<<-1;
else cout<<c;
}
Compilation message
joi2019_ho_t3.cpp:9:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
9 | main(){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
436 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
436 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
940 KB |
Output is correct |
19 |
Correct |
1 ms |
844 KB |
Output is correct |
20 |
Correct |
1 ms |
972 KB |
Output is correct |
21 |
Correct |
2 ms |
844 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
1 ms |
844 KB |
Output is correct |
24 |
Correct |
1 ms |
844 KB |
Output is correct |
25 |
Correct |
1 ms |
972 KB |
Output is correct |
26 |
Correct |
1 ms |
972 KB |
Output is correct |
27 |
Correct |
1 ms |
972 KB |
Output is correct |
28 |
Correct |
1 ms |
844 KB |
Output is correct |
29 |
Correct |
1 ms |
844 KB |
Output is correct |
30 |
Correct |
1 ms |
848 KB |
Output is correct |
31 |
Correct |
1 ms |
792 KB |
Output is correct |
32 |
Correct |
1 ms |
816 KB |
Output is correct |
33 |
Correct |
1 ms |
844 KB |
Output is correct |
34 |
Correct |
1 ms |
844 KB |
Output is correct |
35 |
Correct |
1 ms |
844 KB |
Output is correct |
36 |
Correct |
1 ms |
844 KB |
Output is correct |
37 |
Correct |
1 ms |
844 KB |
Output is correct |
38 |
Correct |
1 ms |
844 KB |
Output is correct |
39 |
Correct |
1 ms |
844 KB |
Output is correct |
40 |
Correct |
1 ms |
460 KB |
Output is correct |
41 |
Correct |
1 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
3148 KB |
Output is correct |
3 |
Correct |
4 ms |
3148 KB |
Output is correct |
4 |
Correct |
5 ms |
3148 KB |
Output is correct |
5 |
Correct |
4 ms |
3252 KB |
Output is correct |
6 |
Correct |
4 ms |
3148 KB |
Output is correct |
7 |
Correct |
4 ms |
3148 KB |
Output is correct |
8 |
Correct |
4 ms |
3148 KB |
Output is correct |
9 |
Correct |
4 ms |
3148 KB |
Output is correct |
10 |
Correct |
4 ms |
3148 KB |
Output is correct |
11 |
Correct |
4 ms |
3148 KB |
Output is correct |
12 |
Correct |
2 ms |
1740 KB |
Output is correct |
13 |
Correct |
2 ms |
2252 KB |
Output is correct |
14 |
Correct |
3 ms |
2636 KB |
Output is correct |
15 |
Correct |
4 ms |
3148 KB |
Output is correct |
16 |
Correct |
5 ms |
3148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
436 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
940 KB |
Output is correct |
19 |
Correct |
1 ms |
844 KB |
Output is correct |
20 |
Correct |
1 ms |
972 KB |
Output is correct |
21 |
Correct |
2 ms |
844 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
1 ms |
844 KB |
Output is correct |
24 |
Correct |
1 ms |
844 KB |
Output is correct |
25 |
Correct |
1 ms |
972 KB |
Output is correct |
26 |
Correct |
1 ms |
972 KB |
Output is correct |
27 |
Correct |
1 ms |
972 KB |
Output is correct |
28 |
Correct |
1 ms |
844 KB |
Output is correct |
29 |
Correct |
1 ms |
844 KB |
Output is correct |
30 |
Correct |
1 ms |
848 KB |
Output is correct |
31 |
Correct |
1 ms |
792 KB |
Output is correct |
32 |
Correct |
1 ms |
816 KB |
Output is correct |
33 |
Correct |
1 ms |
844 KB |
Output is correct |
34 |
Correct |
1 ms |
844 KB |
Output is correct |
35 |
Correct |
1 ms |
844 KB |
Output is correct |
36 |
Correct |
1 ms |
844 KB |
Output is correct |
37 |
Correct |
1 ms |
844 KB |
Output is correct |
38 |
Correct |
1 ms |
844 KB |
Output is correct |
39 |
Correct |
1 ms |
844 KB |
Output is correct |
40 |
Correct |
1 ms |
460 KB |
Output is correct |
41 |
Correct |
1 ms |
716 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
4 ms |
3148 KB |
Output is correct |
44 |
Correct |
4 ms |
3148 KB |
Output is correct |
45 |
Correct |
5 ms |
3148 KB |
Output is correct |
46 |
Correct |
4 ms |
3252 KB |
Output is correct |
47 |
Correct |
4 ms |
3148 KB |
Output is correct |
48 |
Correct |
4 ms |
3148 KB |
Output is correct |
49 |
Correct |
4 ms |
3148 KB |
Output is correct |
50 |
Correct |
4 ms |
3148 KB |
Output is correct |
51 |
Correct |
4 ms |
3148 KB |
Output is correct |
52 |
Correct |
4 ms |
3148 KB |
Output is correct |
53 |
Correct |
2 ms |
1740 KB |
Output is correct |
54 |
Correct |
2 ms |
2252 KB |
Output is correct |
55 |
Correct |
3 ms |
2636 KB |
Output is correct |
56 |
Correct |
4 ms |
3148 KB |
Output is correct |
57 |
Correct |
5 ms |
3148 KB |
Output is correct |
58 |
Correct |
88 ms |
5052 KB |
Output is correct |
59 |
Correct |
76 ms |
5044 KB |
Output is correct |
60 |
Correct |
78 ms |
5196 KB |
Output is correct |
61 |
Correct |
78 ms |
5024 KB |
Output is correct |
62 |
Correct |
9 ms |
4700 KB |
Output is correct |
63 |
Correct |
14 ms |
4572 KB |
Output is correct |
64 |
Correct |
32 ms |
4812 KB |
Output is correct |
65 |
Correct |
44 ms |
4812 KB |
Output is correct |
66 |
Correct |
78 ms |
5032 KB |
Output is correct |
67 |
Correct |
66 ms |
4752 KB |
Output is correct |
68 |
Correct |
74 ms |
5068 KB |
Output is correct |
69 |
Correct |
80 ms |
5144 KB |
Output is correct |
70 |
Correct |
76 ms |
5008 KB |
Output is correct |
71 |
Correct |
89 ms |
5324 KB |
Output is correct |
72 |
Correct |
92 ms |
6664 KB |
Output is correct |
73 |
Correct |
8 ms |
3020 KB |
Output is correct |