#include <bits/stdc++.h>
using namespace std;
int dp[405][405][405][3];
int cnt[405][3],arr[405];
vector<int> inds[3];
const int INF = 1e9+9;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N; cin >> N;
string s; cin >> s; //R is 0, G is 1, Y is 2
int cnt0 = 0, cnt1 = 0, cnt2 = 0;
for(int i = 1;i<=N;++i){
if(s[i-1] == 'R'){
arr[i] = 0;
++cnt0;
}
else if(s[i-1] == 'G'){
arr[i] = 1;
++cnt1;
}
else{
arr[i] = 2;
++cnt2;
}
}
for(int i = 0;i<=cnt0;++i){
for(int j = 0;j<=cnt1;++j){
for(int k = 0;k<=cnt2;++k){
for(int l = 0;l<3;++l){
dp[i][j][k][l] = INF;
}
}
}
}
for(int i = 0;i<3;++i) dp[0][0][0][i] = 0;
for(int i = 1;i<=N;++i){
inds[arr[i]].push_back(i);
for(int j = 0;j<3;++j){
cnt[i][j] = cnt[i-1][j]+(arr[i]==j);
}
}
for(int i = 0;i<N;++i){
for(int j = 0;j<=min(i,cnt0);++j){
for(int k = 0;k<=min(i-j,cnt1);++k){
int l = i-j-k;
if(l > cnt2) continue;
if(j < cnt0){
dp[j+1][k][l][0] = min(dp[j+1][k][l][0],min(dp[j][k][l][1],dp[j][k][l][2])+max(0,cnt[inds[0][j]][1]-k)+max(0,cnt[inds[0][j]][2]-l));
}
if(k < cnt1){
dp[j][k+1][l][1] = min(dp[j][k+1][l][1],min(dp[j][k][l][0],dp[j][k][l][2])+max(0,cnt[inds[1][k]][0]-j)+max(0,cnt[inds[1][k]][2]-l));
}
if(l < cnt2){
dp[j][k][l+1][2] = min(dp[j][k][l+1][2],min(dp[j][k][l][0],dp[j][k][l][1])+max(0,cnt[inds[2][l]][0]-j)+max(0,cnt[inds[2][l]][1]-k));
}
}
}
}
int ans = INF;
for(int i = 0;i<3;++i){
ans = min(ans,dp[cnt0][cnt1][cnt2][i]);
}
if(ans != INF) cout << ans << "\n";
else cout << -1 << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
10844 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
1 ms |
14684 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
20940 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8536 KB |
Output is correct |
16 |
Correct |
2 ms |
23048 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 |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
10844 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
1 ms |
14684 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
20940 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8536 KB |
Output is correct |
16 |
Correct |
2 ms |
23048 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
5 ms |
45656 KB |
Output is correct |
19 |
Correct |
3 ms |
37468 KB |
Output is correct |
20 |
Correct |
5 ms |
49756 KB |
Output is correct |
21 |
Correct |
4 ms |
39516 KB |
Output is correct |
22 |
Correct |
5 ms |
47708 KB |
Output is correct |
23 |
Correct |
3 ms |
33372 KB |
Output is correct |
24 |
Correct |
2 ms |
21084 KB |
Output is correct |
25 |
Correct |
7 ms |
72316 KB |
Output is correct |
26 |
Correct |
5 ms |
62044 KB |
Output is correct |
27 |
Correct |
5 ms |
53852 KB |
Output is correct |
28 |
Correct |
4 ms |
41564 KB |
Output is correct |
29 |
Correct |
5 ms |
43624 KB |
Output is correct |
30 |
Correct |
4 ms |
33372 KB |
Output is correct |
31 |
Correct |
3 ms |
31324 KB |
Output is correct |
32 |
Correct |
4 ms |
35420 KB |
Output is correct |
33 |
Correct |
4 ms |
49756 KB |
Output is correct |
34 |
Correct |
5 ms |
43696 KB |
Output is correct |
35 |
Correct |
5 ms |
45660 KB |
Output is correct |
36 |
Correct |
4 ms |
39516 KB |
Output is correct |
37 |
Correct |
3 ms |
27228 KB |
Output is correct |
38 |
Correct |
3 ms |
29276 KB |
Output is correct |
39 |
Correct |
3 ms |
37468 KB |
Output is correct |
40 |
Correct |
0 ms |
600 KB |
Output is correct |
41 |
Correct |
3 ms |
19076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
58 ms |
229532 KB |
Output is correct |
3 |
Correct |
55 ms |
230228 KB |
Output is correct |
4 |
Correct |
61 ms |
229576 KB |
Output is correct |
5 |
Correct |
61 ms |
229456 KB |
Output is correct |
6 |
Correct |
57 ms |
229584 KB |
Output is correct |
7 |
Correct |
57 ms |
228692 KB |
Output is correct |
8 |
Correct |
67 ms |
228692 KB |
Output is correct |
9 |
Correct |
60 ms |
228176 KB |
Output is correct |
10 |
Correct |
58 ms |
229456 KB |
Output is correct |
11 |
Correct |
58 ms |
229456 KB |
Output is correct |
12 |
Correct |
17 ms |
133976 KB |
Output is correct |
13 |
Correct |
28 ms |
160308 KB |
Output is correct |
14 |
Correct |
38 ms |
186424 KB |
Output is correct |
15 |
Correct |
55 ms |
229204 KB |
Output is correct |
16 |
Correct |
60 ms |
230480 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 |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
10844 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
1 ms |
14684 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
20940 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8536 KB |
Output is correct |
16 |
Correct |
2 ms |
23048 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
5 ms |
45656 KB |
Output is correct |
19 |
Correct |
3 ms |
37468 KB |
Output is correct |
20 |
Correct |
5 ms |
49756 KB |
Output is correct |
21 |
Correct |
4 ms |
39516 KB |
Output is correct |
22 |
Correct |
5 ms |
47708 KB |
Output is correct |
23 |
Correct |
3 ms |
33372 KB |
Output is correct |
24 |
Correct |
2 ms |
21084 KB |
Output is correct |
25 |
Correct |
7 ms |
72316 KB |
Output is correct |
26 |
Correct |
5 ms |
62044 KB |
Output is correct |
27 |
Correct |
5 ms |
53852 KB |
Output is correct |
28 |
Correct |
4 ms |
41564 KB |
Output is correct |
29 |
Correct |
5 ms |
43624 KB |
Output is correct |
30 |
Correct |
4 ms |
33372 KB |
Output is correct |
31 |
Correct |
3 ms |
31324 KB |
Output is correct |
32 |
Correct |
4 ms |
35420 KB |
Output is correct |
33 |
Correct |
4 ms |
49756 KB |
Output is correct |
34 |
Correct |
5 ms |
43696 KB |
Output is correct |
35 |
Correct |
5 ms |
45660 KB |
Output is correct |
36 |
Correct |
4 ms |
39516 KB |
Output is correct |
37 |
Correct |
3 ms |
27228 KB |
Output is correct |
38 |
Correct |
3 ms |
29276 KB |
Output is correct |
39 |
Correct |
3 ms |
37468 KB |
Output is correct |
40 |
Correct |
0 ms |
600 KB |
Output is correct |
41 |
Correct |
3 ms |
19076 KB |
Output is correct |
42 |
Correct |
1 ms |
4440 KB |
Output is correct |
43 |
Correct |
58 ms |
229532 KB |
Output is correct |
44 |
Correct |
55 ms |
230228 KB |
Output is correct |
45 |
Correct |
61 ms |
229576 KB |
Output is correct |
46 |
Correct |
61 ms |
229456 KB |
Output is correct |
47 |
Correct |
57 ms |
229584 KB |
Output is correct |
48 |
Correct |
57 ms |
228692 KB |
Output is correct |
49 |
Correct |
67 ms |
228692 KB |
Output is correct |
50 |
Correct |
60 ms |
228176 KB |
Output is correct |
51 |
Correct |
58 ms |
229456 KB |
Output is correct |
52 |
Correct |
58 ms |
229456 KB |
Output is correct |
53 |
Correct |
17 ms |
133976 KB |
Output is correct |
54 |
Correct |
28 ms |
160308 KB |
Output is correct |
55 |
Correct |
38 ms |
186424 KB |
Output is correct |
56 |
Correct |
55 ms |
229204 KB |
Output is correct |
57 |
Correct |
60 ms |
230480 KB |
Output is correct |
58 |
Correct |
52 ms |
154628 KB |
Output is correct |
59 |
Correct |
60 ms |
171860 KB |
Output is correct |
60 |
Correct |
57 ms |
166232 KB |
Output is correct |
61 |
Correct |
57 ms |
162140 KB |
Output is correct |
62 |
Correct |
56 ms |
230992 KB |
Output is correct |
63 |
Correct |
57 ms |
227696 KB |
Output is correct |
64 |
Correct |
64 ms |
217488 KB |
Output is correct |
65 |
Correct |
68 ms |
203092 KB |
Output is correct |
66 |
Correct |
58 ms |
163676 KB |
Output is correct |
67 |
Correct |
53 ms |
154196 KB |
Output is correct |
68 |
Correct |
58 ms |
165208 KB |
Output is correct |
69 |
Correct |
56 ms |
163164 KB |
Output is correct |
70 |
Correct |
58 ms |
168280 KB |
Output is correct |
71 |
Correct |
57 ms |
166044 KB |
Output is correct |
72 |
Correct |
34 ms |
148560 KB |
Output is correct |
73 |
Correct |
3 ms |
8792 KB |
Output is correct |