#include<bits/stdc++.h>
using namespace std;
const int N = 400;
const int inf = 2e9;
char v[N];
int f[3];
vector<vector<vector<vector<int>>>> dp;
vector <int> pos[3];
int v2[N][3];
int get(int i,int j,int l,int id){
int ans = 0;
int pos2;
if(id == 0){
pos2 = pos[id][i];
}else if(id == 1){
pos2 = pos[id][j];
}else{
pos2 = pos[id][l];
}
if(id != 0){
ans+=max(i - v2[pos2][0],0);
}
if(id != 1){
ans+=max(j - v2[pos2][1],0);
}
if(id != 2){
ans+=max(l - v2[pos2][2],0);
}
//cout<<i<<' '<<j<<' '<<l<<' '<<id<<' '<<ans<<'\n';
return ans;
}
int main(){
int n;
cin>>n;
for(int i = 0;i < n;i++){
cin>>v[i];
v[i] = (v[i] == 'R'?0:(v[i] == 'G'?1:2));
f[v[i]]++;
pos[v[i]].push_back(i);
for(int j = 0;j < 3;j++){
v2[i][j] = (int)pos[j].size();
}
}
dp.assign(f[0] + 1,vector<vector<vector<int>>>(f[1] + 1,vector<vector<int>>(f[2] + 1,vector<int>(3,inf))));
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
dp[0][0][0][0] = 0;
for(int i = 0;i <= f[0];i++){
for(int j = 0;j <= f[1];j++){
for(int l = 0;l <= f[2];l++){
for(int k = 0;k < 3;k++){
if(i != f[0] && k != 0){
dp[i + 1][j][l][0] = min(dp[i + 1][j][l][0],dp[i][j][l][k] + get(i,j,l,0));
}
if(j != f[1] && k != 1){
dp[i][j + 1][l][1] = min(dp[i][j + 1][l][1],dp[i][j][l][k] + get(i,j,l,1));
}
if(l != f[2] && k != 2){
dp[i][j][l + 1][2] = min(dp[i][j][l + 1][2],dp[i][j][l][k] + get(i,j,l,2));
}
}
}
}
}
int ans = min({dp[f[0]][f[1]][f[2]][0],dp[f[0]][f[1]][f[2]][1],dp[f[0]][f[1]][f[2]][2]});
if(ans == inf)ans = -1;
cout<<ans;
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:38:14: warning: array subscript has type 'char' [-Wchar-subscripts]
38 | f[v[i]]++;
| ~~~^
joi2019_ho_t3.cpp:39:16: warning: array subscript has type 'char' [-Wchar-subscripts]
39 | pos[v[i]].push_back(i);
| ~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
440 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
436 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
440 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
440 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
436 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
440 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
896 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
0 ms |
440 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
692 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
1 ms |
856 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
4 ms |
3928 KB |
Output is correct |
3 |
Correct |
4 ms |
3676 KB |
Output is correct |
4 |
Correct |
4 ms |
3932 KB |
Output is correct |
5 |
Correct |
5 ms |
3932 KB |
Output is correct |
6 |
Correct |
4 ms |
3932 KB |
Output is correct |
7 |
Correct |
4 ms |
3768 KB |
Output is correct |
8 |
Correct |
4 ms |
3676 KB |
Output is correct |
9 |
Correct |
4 ms |
3668 KB |
Output is correct |
10 |
Correct |
4 ms |
3932 KB |
Output is correct |
11 |
Correct |
4 ms |
3948 KB |
Output is correct |
12 |
Correct |
2 ms |
1372 KB |
Output is correct |
13 |
Correct |
3 ms |
1884 KB |
Output is correct |
14 |
Correct |
3 ms |
2652 KB |
Output is correct |
15 |
Correct |
4 ms |
3940 KB |
Output is correct |
16 |
Correct |
5 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
440 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
436 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
440 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
896 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
0 ms |
440 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
692 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
1 ms |
856 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
856 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
4 ms |
3928 KB |
Output is correct |
44 |
Correct |
4 ms |
3676 KB |
Output is correct |
45 |
Correct |
4 ms |
3932 KB |
Output is correct |
46 |
Correct |
5 ms |
3932 KB |
Output is correct |
47 |
Correct |
4 ms |
3932 KB |
Output is correct |
48 |
Correct |
4 ms |
3768 KB |
Output is correct |
49 |
Correct |
4 ms |
3676 KB |
Output is correct |
50 |
Correct |
4 ms |
3668 KB |
Output is correct |
51 |
Correct |
4 ms |
3932 KB |
Output is correct |
52 |
Correct |
4 ms |
3948 KB |
Output is correct |
53 |
Correct |
2 ms |
1372 KB |
Output is correct |
54 |
Correct |
3 ms |
1884 KB |
Output is correct |
55 |
Correct |
3 ms |
2652 KB |
Output is correct |
56 |
Correct |
4 ms |
3940 KB |
Output is correct |
57 |
Correct |
5 ms |
3932 KB |
Output is correct |
58 |
Correct |
184 ms |
133200 KB |
Output is correct |
59 |
Correct |
176 ms |
133248 KB |
Output is correct |
60 |
Correct |
231 ms |
132436 KB |
Output is correct |
61 |
Correct |
180 ms |
133840 KB |
Output is correct |
62 |
Correct |
14 ms |
12376 KB |
Output is correct |
63 |
Correct |
26 ms |
20828 KB |
Output is correct |
64 |
Correct |
92 ms |
66732 KB |
Output is correct |
65 |
Correct |
128 ms |
95572 KB |
Output is correct |
66 |
Correct |
174 ms |
131668 KB |
Output is correct |
67 |
Correct |
185 ms |
131664 KB |
Output is correct |
68 |
Correct |
220 ms |
133720 KB |
Output is correct |
69 |
Correct |
182 ms |
134736 KB |
Output is correct |
70 |
Correct |
181 ms |
134192 KB |
Output is correct |
71 |
Correct |
181 ms |
130888 KB |
Output is correct |
72 |
Correct |
47 ms |
34140 KB |
Output is correct |
73 |
Correct |
13 ms |
10584 KB |
Output is correct |