#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e15;
int pref[3][401][401];
int DP[401][401][401][3];
int idx[3][401];
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
int red = 0;
int green = 0;
int yellow = 0;
for (int i = 1; i <= n; i ++) {
char a;cin>>a;
if(a=='R'){
idx[0][++red]=i;
for(int j=1;j<=n;j++)pref[0][red][j]=pref[0][red-1][j];
for(int j=i;j<=n;j++)pref[0][red][j]++;
} else if(a=='G'){
idx[1][++green]=i;
for(int j=1;j<=n;j++)pref[1][green][j]=pref[1][green-1][j];
for(int j=i;j<=n;j++)pref[1][green][j]++;
} else {
idx[2][++yellow]=i;
for(int j=1;j<=n;j++)pref[2][yellow][j]=pref[2][yellow-1][j];
for(int j=i;j<=n;j++)pref[2][yellow][j]++;
}
}
for(int i=0;i<=red;i++){
for(int j=0;j<=green;j++){
for(int k=0;k<=yellow;k++){
if(i==0 and j==0 and k==0)continue;
// If last is red
if(i==0)DP[i][j][k][0]=INF;
else {
DP[i][j][k][0]=min(DP[i-1][j][k][1],DP[i-1][j][k][2])+idx[0][i]-1-pref[0][i][idx[0][i]-1]-pref[1][j][idx[0][i]-1]-pref[2][k][idx[0][i]-1];
}
// If last is green
if(j==0)DP[i][j][k][1]=INF;
else {
DP[i][j][k][1]=min(DP[i][j-1][k][0],DP[i][j-1][k][2])+idx[1][j]-1-pref[0][i][idx[1][j]-1]-pref[1][j][idx[1][j]-1]-pref[2][k][idx[1][j]-1];
}
// If last is yellow
if(k==0)DP[i][j][k][2]=INF;
else {
DP[i][j][k][2]=min(DP[i][j][k-1][0],DP[i][j][k-1][1])+idx[2][k]-1-pref[0][i][idx[2][k]-1]-pref[1][j][idx[2][k]-1]-pref[2][k][idx[2][k]-1];
}
}
}
}
int ans = min({DP[red][green][yellow][0],DP[red][green][yellow][1],DP[red][green][yellow][2]});
cout << (ans>=INF ? -1 : ans) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6624 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14888 KB |
Output is correct |
8 |
Correct |
2 ms |
12632 KB |
Output is correct |
9 |
Correct |
2 ms |
12888 KB |
Output is correct |
10 |
Correct |
2 ms |
20828 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10584 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6624 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14888 KB |
Output is correct |
8 |
Correct |
2 ms |
12632 KB |
Output is correct |
9 |
Correct |
2 ms |
12888 KB |
Output is correct |
10 |
Correct |
2 ms |
20828 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10584 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
6 ms |
51804 KB |
Output is correct |
19 |
Correct |
5 ms |
43612 KB |
Output is correct |
20 |
Correct |
6 ms |
55896 KB |
Output is correct |
21 |
Correct |
5 ms |
47824 KB |
Output is correct |
22 |
Correct |
6 ms |
53852 KB |
Output is correct |
23 |
Correct |
5 ms |
41564 KB |
Output is correct |
24 |
Correct |
3 ms |
25180 KB |
Output is correct |
25 |
Correct |
9 ms |
86840 KB |
Output is correct |
26 |
Correct |
8 ms |
76632 KB |
Output is correct |
27 |
Correct |
7 ms |
64348 KB |
Output is correct |
28 |
Correct |
5 ms |
49756 KB |
Output is correct |
29 |
Correct |
6 ms |
49756 KB |
Output is correct |
30 |
Correct |
5 ms |
41560 KB |
Output is correct |
31 |
Correct |
4 ms |
37720 KB |
Output is correct |
32 |
Correct |
5 ms |
43612 KB |
Output is correct |
33 |
Correct |
7 ms |
62300 KB |
Output is correct |
34 |
Correct |
6 ms |
53852 KB |
Output is correct |
35 |
Correct |
6 ms |
53988 KB |
Output is correct |
36 |
Correct |
5 ms |
45656 KB |
Output is correct |
37 |
Correct |
4 ms |
33372 KB |
Output is correct |
38 |
Correct |
4 ms |
35620 KB |
Output is correct |
39 |
Correct |
6 ms |
45660 KB |
Output is correct |
40 |
Correct |
1 ms |
2652 KB |
Output is correct |
41 |
Correct |
3 ms |
25180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
189 ms |
759868 KB |
Output is correct |
3 |
Correct |
82 ms |
760144 KB |
Output is correct |
4 |
Correct |
76 ms |
758864 KB |
Output is correct |
5 |
Correct |
77 ms |
758864 KB |
Output is correct |
6 |
Correct |
76 ms |
758868 KB |
Output is correct |
7 |
Correct |
76 ms |
756832 KB |
Output is correct |
8 |
Correct |
75 ms |
756820 KB |
Output is correct |
9 |
Correct |
76 ms |
756816 KB |
Output is correct |
10 |
Correct |
75 ms |
758864 KB |
Output is correct |
11 |
Correct |
76 ms |
758832 KB |
Output is correct |
12 |
Correct |
32 ms |
314960 KB |
Output is correct |
13 |
Correct |
47 ms |
458848 KB |
Output is correct |
14 |
Correct |
58 ms |
600660 KB |
Output is correct |
15 |
Correct |
76 ms |
756704 KB |
Output is correct |
16 |
Correct |
76 ms |
756836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6624 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14888 KB |
Output is correct |
8 |
Correct |
2 ms |
12632 KB |
Output is correct |
9 |
Correct |
2 ms |
12888 KB |
Output is correct |
10 |
Correct |
2 ms |
20828 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10584 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
6 ms |
51804 KB |
Output is correct |
19 |
Correct |
5 ms |
43612 KB |
Output is correct |
20 |
Correct |
6 ms |
55896 KB |
Output is correct |
21 |
Correct |
5 ms |
47824 KB |
Output is correct |
22 |
Correct |
6 ms |
53852 KB |
Output is correct |
23 |
Correct |
5 ms |
41564 KB |
Output is correct |
24 |
Correct |
3 ms |
25180 KB |
Output is correct |
25 |
Correct |
9 ms |
86840 KB |
Output is correct |
26 |
Correct |
8 ms |
76632 KB |
Output is correct |
27 |
Correct |
7 ms |
64348 KB |
Output is correct |
28 |
Correct |
5 ms |
49756 KB |
Output is correct |
29 |
Correct |
6 ms |
49756 KB |
Output is correct |
30 |
Correct |
5 ms |
41560 KB |
Output is correct |
31 |
Correct |
4 ms |
37720 KB |
Output is correct |
32 |
Correct |
5 ms |
43612 KB |
Output is correct |
33 |
Correct |
7 ms |
62300 KB |
Output is correct |
34 |
Correct |
6 ms |
53852 KB |
Output is correct |
35 |
Correct |
6 ms |
53988 KB |
Output is correct |
36 |
Correct |
5 ms |
45656 KB |
Output is correct |
37 |
Correct |
4 ms |
33372 KB |
Output is correct |
38 |
Correct |
4 ms |
35620 KB |
Output is correct |
39 |
Correct |
6 ms |
45660 KB |
Output is correct |
40 |
Correct |
1 ms |
2652 KB |
Output is correct |
41 |
Correct |
3 ms |
25180 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
189 ms |
759868 KB |
Output is correct |
44 |
Correct |
82 ms |
760144 KB |
Output is correct |
45 |
Correct |
76 ms |
758864 KB |
Output is correct |
46 |
Correct |
77 ms |
758864 KB |
Output is correct |
47 |
Correct |
76 ms |
758868 KB |
Output is correct |
48 |
Correct |
76 ms |
756832 KB |
Output is correct |
49 |
Correct |
75 ms |
756820 KB |
Output is correct |
50 |
Correct |
76 ms |
756816 KB |
Output is correct |
51 |
Correct |
75 ms |
758864 KB |
Output is correct |
52 |
Correct |
76 ms |
758832 KB |
Output is correct |
53 |
Correct |
32 ms |
314960 KB |
Output is correct |
54 |
Correct |
47 ms |
458848 KB |
Output is correct |
55 |
Correct |
58 ms |
600660 KB |
Output is correct |
56 |
Correct |
76 ms |
756704 KB |
Output is correct |
57 |
Correct |
76 ms |
756836 KB |
Output is correct |
58 |
Correct |
52 ms |
404000 KB |
Output is correct |
59 |
Correct |
64 ms |
486224 KB |
Output is correct |
60 |
Correct |
63 ms |
465444 KB |
Output is correct |
61 |
Correct |
57 ms |
445012 KB |
Output is correct |
62 |
Correct |
81 ms |
794924 KB |
Output is correct |
63 |
Correct |
76 ms |
740436 KB |
Output is correct |
64 |
Correct |
78 ms |
703484 KB |
Output is correct |
65 |
Correct |
71 ms |
605072 KB |
Output is correct |
66 |
Correct |
57 ms |
452688 KB |
Output is correct |
67 |
Correct |
56 ms |
391504 KB |
Output is correct |
68 |
Correct |
58 ms |
453204 KB |
Output is correct |
69 |
Correct |
58 ms |
449104 KB |
Output is correct |
70 |
Correct |
59 ms |
461396 KB |
Output is correct |
71 |
Correct |
64 ms |
477780 KB |
Output is correct |
72 |
Correct |
72 ms |
699456 KB |
Output is correct |
73 |
Correct |
3 ms |
16220 KB |
Output is correct |