#include<bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define ppb pop_back
#define ii pair<int,int>
#define ll long long
#define umin(x,y) x=min(x,y)
#define umax(x,y) x=max(x,y)
#define orta ((bas+son)>>1)
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define inf 2000000000
#define N 3005
#define MOD 998244353
using namespace std;
int n;
int a[405];
int dp[405][205][205][4];
int shift[5][5][405][405];
int pos[5][405];
int tot[5];
char s[405];
int f(int cur,int cnt1,int cnt2,int last) {
if(cur==n+1) return 0;
int& r=dp[cur][cnt1][cnt2][last];
if(~r) return r;
r=inf;
int cnt3=(cur-1-cnt1-cnt2);
if(last!=1) {
if(cnt1!=tot[1]) {
int add=shift[1][2][cnt1+1][cnt2]+shift[1][3][cnt1+1][cnt3];
umin(r,f(cur+1,cnt1+1,cnt2,1)+add);
}
}
if(last!=2) {
if(cnt2!=tot[2]) {
int add=shift[2][1][cnt2+1][cnt1]+shift[2][3][cnt2+1][cnt3];
umin(r,f(cur+1,cnt1,cnt2+1,2)+add);
}
}
if(last!=3) {
if(cnt3!=tot[3]) {
int add=shift[3][1][cnt3+1][cnt1]+shift[3][2][cnt3+1][cnt2];
umin(r,f(cur+1,cnt1,cnt2,3)+add);
}
}
return r;
}
void pre() {
for(int i=1;i<=n;i++) {
a[i]=(s[i]=='R'?1:s[i]=='G'?2:3);
++tot[a[i]];
pos[a[i]][tot[a[i]]]=i;
}
for(int l1=1;l1<=3;l1++) {
for(int l2=1;l2<=3;l2++) {
if(l1==l2) continue ;
for(int c1=0;c1<=tot[l1];c1++) {
int cnt=0;
for(int c2=0;c2<=tot[l2];c2++) {
cnt+=(pos[l1][c1]<pos[l2][c2]);
shift[l1][l2][c1][c2]=cnt;
}
}
}
}
}
void valid() {
int cnt[99]={0};
for(int i=1;i<=n;i++) {
cnt[s[i]]++;
}
if(*max_element(cnt,cnt+99)>(n+1)/2) {
printf("-1");
exit(0);
}
}
int main() {
scanf("%d",&n);
scanf("%s",s+1);
valid();
pre();
memset(dp,-1,sizeof(dp));
printf("%d",f(1,0,0,0));
}
Compilation message
joi2019_ho_t3.cpp: In function 'void valid()':
joi2019_ho_t3.cpp:122:11: warning: array subscript has type 'char' [-Wchar-subscripts]
cnt[s[i]]++;
^
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:138:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
joi2019_ho_t3.cpp:140:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s",s+1);
~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
220 ms |
266744 KB |
Output is correct |
2 |
Correct |
220 ms |
266776 KB |
Output is correct |
3 |
Correct |
218 ms |
266872 KB |
Output is correct |
4 |
Correct |
218 ms |
266872 KB |
Output is correct |
5 |
Correct |
231 ms |
266872 KB |
Output is correct |
6 |
Correct |
221 ms |
266872 KB |
Output is correct |
7 |
Correct |
219 ms |
266896 KB |
Output is correct |
8 |
Correct |
220 ms |
266872 KB |
Output is correct |
9 |
Correct |
228 ms |
266932 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
223 ms |
266944 KB |
Output is correct |
12 |
Correct |
220 ms |
266872 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
219 ms |
266940 KB |
Output is correct |
15 |
Correct |
221 ms |
266872 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
220 ms |
266744 KB |
Output is correct |
2 |
Correct |
220 ms |
266776 KB |
Output is correct |
3 |
Correct |
218 ms |
266872 KB |
Output is correct |
4 |
Correct |
218 ms |
266872 KB |
Output is correct |
5 |
Correct |
231 ms |
266872 KB |
Output is correct |
6 |
Correct |
221 ms |
266872 KB |
Output is correct |
7 |
Correct |
219 ms |
266896 KB |
Output is correct |
8 |
Correct |
220 ms |
266872 KB |
Output is correct |
9 |
Correct |
228 ms |
266932 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
223 ms |
266944 KB |
Output is correct |
12 |
Correct |
220 ms |
266872 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
219 ms |
266940 KB |
Output is correct |
15 |
Correct |
221 ms |
266872 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
223 ms |
267000 KB |
Output is correct |
19 |
Correct |
222 ms |
267076 KB |
Output is correct |
20 |
Correct |
223 ms |
267096 KB |
Output is correct |
21 |
Correct |
220 ms |
267168 KB |
Output is correct |
22 |
Correct |
221 ms |
267000 KB |
Output is correct |
23 |
Correct |
219 ms |
267044 KB |
Output is correct |
24 |
Correct |
225 ms |
267128 KB |
Output is correct |
25 |
Correct |
2 ms |
256 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
220 ms |
266948 KB |
Output is correct |
28 |
Correct |
221 ms |
267000 KB |
Output is correct |
29 |
Correct |
226 ms |
267180 KB |
Output is correct |
30 |
Correct |
221 ms |
267060 KB |
Output is correct |
31 |
Correct |
243 ms |
267004 KB |
Output is correct |
32 |
Correct |
244 ms |
267140 KB |
Output is correct |
33 |
Correct |
2 ms |
256 KB |
Output is correct |
34 |
Correct |
2 ms |
256 KB |
Output is correct |
35 |
Correct |
222 ms |
267028 KB |
Output is correct |
36 |
Correct |
220 ms |
267096 KB |
Output is correct |
37 |
Correct |
265 ms |
267000 KB |
Output is correct |
38 |
Correct |
232 ms |
267128 KB |
Output is correct |
39 |
Correct |
221 ms |
267028 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
271 ms |
267000 KB |
Output is correct |
2 |
Correct |
254 ms |
268236 KB |
Output is correct |
3 |
Correct |
224 ms |
268252 KB |
Output is correct |
4 |
Correct |
223 ms |
268252 KB |
Output is correct |
5 |
Correct |
219 ms |
268152 KB |
Output is correct |
6 |
Correct |
253 ms |
268152 KB |
Output is correct |
7 |
Correct |
221 ms |
268152 KB |
Output is correct |
8 |
Correct |
220 ms |
268152 KB |
Output is correct |
9 |
Correct |
257 ms |
268212 KB |
Output is correct |
10 |
Correct |
220 ms |
268260 KB |
Output is correct |
11 |
Correct |
217 ms |
268176 KB |
Output is correct |
12 |
Correct |
221 ms |
267572 KB |
Output is correct |
13 |
Correct |
233 ms |
267756 KB |
Output is correct |
14 |
Correct |
219 ms |
268028 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
220 ms |
266744 KB |
Output is correct |
2 |
Correct |
220 ms |
266776 KB |
Output is correct |
3 |
Correct |
218 ms |
266872 KB |
Output is correct |
4 |
Correct |
218 ms |
266872 KB |
Output is correct |
5 |
Correct |
231 ms |
266872 KB |
Output is correct |
6 |
Correct |
221 ms |
266872 KB |
Output is correct |
7 |
Correct |
219 ms |
266896 KB |
Output is correct |
8 |
Correct |
220 ms |
266872 KB |
Output is correct |
9 |
Correct |
228 ms |
266932 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
223 ms |
266944 KB |
Output is correct |
12 |
Correct |
220 ms |
266872 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
219 ms |
266940 KB |
Output is correct |
15 |
Correct |
221 ms |
266872 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
223 ms |
267000 KB |
Output is correct |
19 |
Correct |
222 ms |
267076 KB |
Output is correct |
20 |
Correct |
223 ms |
267096 KB |
Output is correct |
21 |
Correct |
220 ms |
267168 KB |
Output is correct |
22 |
Correct |
221 ms |
267000 KB |
Output is correct |
23 |
Correct |
219 ms |
267044 KB |
Output is correct |
24 |
Correct |
225 ms |
267128 KB |
Output is correct |
25 |
Correct |
2 ms |
256 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
220 ms |
266948 KB |
Output is correct |
28 |
Correct |
221 ms |
267000 KB |
Output is correct |
29 |
Correct |
226 ms |
267180 KB |
Output is correct |
30 |
Correct |
221 ms |
267060 KB |
Output is correct |
31 |
Correct |
243 ms |
267004 KB |
Output is correct |
32 |
Correct |
244 ms |
267140 KB |
Output is correct |
33 |
Correct |
2 ms |
256 KB |
Output is correct |
34 |
Correct |
2 ms |
256 KB |
Output is correct |
35 |
Correct |
222 ms |
267028 KB |
Output is correct |
36 |
Correct |
220 ms |
267096 KB |
Output is correct |
37 |
Correct |
265 ms |
267000 KB |
Output is correct |
38 |
Correct |
232 ms |
267128 KB |
Output is correct |
39 |
Correct |
221 ms |
267028 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
256 KB |
Output is correct |
42 |
Correct |
271 ms |
267000 KB |
Output is correct |
43 |
Correct |
254 ms |
268236 KB |
Output is correct |
44 |
Correct |
224 ms |
268252 KB |
Output is correct |
45 |
Correct |
223 ms |
268252 KB |
Output is correct |
46 |
Correct |
219 ms |
268152 KB |
Output is correct |
47 |
Correct |
253 ms |
268152 KB |
Output is correct |
48 |
Correct |
221 ms |
268152 KB |
Output is correct |
49 |
Correct |
220 ms |
268152 KB |
Output is correct |
50 |
Correct |
257 ms |
268212 KB |
Output is correct |
51 |
Correct |
220 ms |
268260 KB |
Output is correct |
52 |
Correct |
217 ms |
268176 KB |
Output is correct |
53 |
Correct |
221 ms |
267572 KB |
Output is correct |
54 |
Correct |
233 ms |
267756 KB |
Output is correct |
55 |
Correct |
219 ms |
268028 KB |
Output is correct |
56 |
Correct |
2 ms |
256 KB |
Output is correct |
57 |
Correct |
2 ms |
376 KB |
Output is correct |
58 |
Correct |
393 ms |
268144 KB |
Output is correct |
59 |
Correct |
372 ms |
268124 KB |
Output is correct |
60 |
Correct |
412 ms |
268472 KB |
Output is correct |
61 |
Correct |
390 ms |
268252 KB |
Output is correct |
62 |
Correct |
2 ms |
376 KB |
Output is correct |
63 |
Correct |
222 ms |
268200 KB |
Output is correct |
64 |
Correct |
240 ms |
268156 KB |
Output is correct |
65 |
Correct |
277 ms |
268280 KB |
Output is correct |
66 |
Correct |
372 ms |
268280 KB |
Output is correct |
67 |
Correct |
371 ms |
268280 KB |
Output is correct |
68 |
Correct |
376 ms |
268232 KB |
Output is correct |
69 |
Correct |
383 ms |
268280 KB |
Output is correct |
70 |
Correct |
378 ms |
268068 KB |
Output is correct |
71 |
Correct |
362 ms |
268280 KB |
Output is correct |
72 |
Correct |
2 ms |
256 KB |
Output is correct |
73 |
Correct |
2 ms |
364 KB |
Output is correct |