#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(),x.end()
#define infl 0x3f3f3f3f3f3f3f3f
#define ll long long
template<typename T> void chmin(T& x, T y) { x=min(x,y); }
#define N 400
int n;
char s[N+1];
int pos[3][N+1];
int cnt[3][N+1][3];
int main()
{
scanf("%d %s",&n,s);
for(auto&x:s) x = x=='R'?0:x=='G'?1:x=='Y'?2:-1;
int c[3]={0,0,0};
for(int i=0;i<n;i++) {
pos[s[i]][c[s[i]]]=i;
for(int k=0;k<3;k++)
cnt[s[i]][c[s[i]]][k]=c[k];
c[s[i]]++;
}
vector<vector<vector<vector<ll>>>> dp(c[0]+1,vector<vector<vector<ll>>>(c[1]+1,vector<vector<ll>>(c[2]+1,vector<ll>(3,infl))));
//memset(dp,0x3f,sizeof(dp));
for(int k=0;k<3;k++) dp[0][0][0][k]=0;
for(int i0=0;i0<=c[0];i0++)
for(int i1=0;i1<=c[1];i1++)
for(int i2=0;i2<=c[2];i2++) {
for(int j=0;j<3;j++) {
ll v=dp[i0][i1][i2][j];
for(int k=0;k<3;k++) if(j!=k) {
int i[3]={i0,i1,i2};
ll u = v;
for(int l=0;l<3;l++)u += max(0, cnt[k][i[k]][l] - i[l]);
i[k]++;
for(int ii=0;ii<3;ii++)if(i[ii]>c[ii])goto dont;
chmin(dp[i[0]][i[1]][i[2]][k],u);
dont:;
}
}
}
ll mn = infl;
for(int k=0;k<3;k++) chmin(mn,dp[c[0]][c[1]][c[2]][k]);
if(mn >= infl>>1)printf("-1\n");
else printf("%lld\n",mn);
}
// then to optimize this, we can consider trying everything at once
// so dp[ra][ga][ba][current symbol] will trivialize this
// why didn't i think of this hadsfhlaksdhflkasdjalkfjslkdajflkadsj
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:23:24: warning: array subscript has type 'char' [-Wchar-subscripts]
23 | pos[s[i]][c[s[i]]]=i;
| ~~~^
joi2019_ho_t3.cpp:23:32: warning: array subscript has type 'char' [-Wchar-subscripts]
23 | pos[s[i]][c[s[i]]]=i;
| ~~~^
joi2019_ho_t3.cpp:25:32: warning: array subscript has type 'char' [-Wchar-subscripts]
25 | cnt[s[i]][c[s[i]]][k]=c[k];
| ~~~^
joi2019_ho_t3.cpp:25:40: warning: array subscript has type 'char' [-Wchar-subscripts]
25 | cnt[s[i]][c[s[i]]][k]=c[k];
| ~~~^
joi2019_ho_t3.cpp:26:22: warning: array subscript has type 'char' [-Wchar-subscripts]
26 | c[s[i]]++;
| ~~~^
joi2019_ho_t3.cpp:18:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | scanf("%d %s",&n,s);
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
2 ms |
956 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
2 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 |
856 KB |
Output is correct |
32 |
Correct |
1 ms |
856 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
856 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
2 ms |
860 KB |
Output is correct |
38 |
Correct |
2 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
3928 KB |
Output is correct |
3 |
Correct |
5 ms |
3936 KB |
Output is correct |
4 |
Correct |
5 ms |
3936 KB |
Output is correct |
5 |
Correct |
5 ms |
3936 KB |
Output is correct |
6 |
Correct |
5 ms |
3932 KB |
Output is correct |
7 |
Correct |
5 ms |
3932 KB |
Output is correct |
8 |
Correct |
5 ms |
3900 KB |
Output is correct |
9 |
Correct |
5 ms |
3672 KB |
Output is correct |
10 |
Correct |
5 ms |
3936 KB |
Output is correct |
11 |
Correct |
5 ms |
3932 KB |
Output is correct |
12 |
Correct |
2 ms |
1372 KB |
Output is correct |
13 |
Correct |
4 ms |
1880 KB |
Output is correct |
14 |
Correct |
5 ms |
2652 KB |
Output is correct |
15 |
Correct |
5 ms |
3884 KB |
Output is correct |
16 |
Correct |
6 ms |
3932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
2 ms |
956 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
2 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 |
856 KB |
Output is correct |
32 |
Correct |
1 ms |
856 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
856 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
2 ms |
860 KB |
Output is correct |
38 |
Correct |
2 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
444 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
5 ms |
3928 KB |
Output is correct |
44 |
Correct |
5 ms |
3936 KB |
Output is correct |
45 |
Correct |
5 ms |
3936 KB |
Output is correct |
46 |
Correct |
5 ms |
3936 KB |
Output is correct |
47 |
Correct |
5 ms |
3932 KB |
Output is correct |
48 |
Correct |
5 ms |
3932 KB |
Output is correct |
49 |
Correct |
5 ms |
3900 KB |
Output is correct |
50 |
Correct |
5 ms |
3672 KB |
Output is correct |
51 |
Correct |
5 ms |
3936 KB |
Output is correct |
52 |
Correct |
5 ms |
3932 KB |
Output is correct |
53 |
Correct |
2 ms |
1372 KB |
Output is correct |
54 |
Correct |
4 ms |
1880 KB |
Output is correct |
55 |
Correct |
5 ms |
2652 KB |
Output is correct |
56 |
Correct |
5 ms |
3884 KB |
Output is correct |
57 |
Correct |
6 ms |
3932 KB |
Output is correct |
58 |
Correct |
273 ms |
133328 KB |
Output is correct |
59 |
Correct |
283 ms |
133488 KB |
Output is correct |
60 |
Correct |
274 ms |
132688 KB |
Output is correct |
61 |
Correct |
282 ms |
134092 KB |
Output is correct |
62 |
Correct |
22 ms |
12380 KB |
Output is correct |
63 |
Correct |
40 ms |
20828 KB |
Output is correct |
64 |
Correct |
155 ms |
66748 KB |
Output is correct |
65 |
Correct |
204 ms |
95756 KB |
Output is correct |
66 |
Correct |
279 ms |
131904 KB |
Output is correct |
67 |
Correct |
270 ms |
131924 KB |
Output is correct |
68 |
Correct |
281 ms |
133880 KB |
Output is correct |
69 |
Correct |
274 ms |
134760 KB |
Output is correct |
70 |
Correct |
275 ms |
134304 KB |
Output is correct |
71 |
Correct |
265 ms |
130900 KB |
Output is correct |
72 |
Correct |
73 ms |
34016 KB |
Output is correct |
73 |
Correct |
19 ms |
10724 KB |
Output is correct |