#include<bits/stdc++.h>
using namespace std;
const int nmax=400+5;
int n;
int inp[nmax];
int where[3][nmax],pointer[3];
int dp[3][nmax/2][nmax/2][nmax/2];
int cnt[3][nmax];
int rec(int prv,int cnt_0,int cnt_1,int cnt_2)
{
//cout<<prv<<" "<<cnt_0<<" "<<cnt_1<<" "<<cnt_2<<endl;
if(cnt_0==pointer[0]&&cnt_1==pointer[1]&&cnt_2==pointer[2])return 0;
if(prv!=-1&&dp[prv][cnt_0][cnt_1][cnt_2]!=-1)return dp[prv][cnt_0][cnt_1][cnt_2];
int seen[3]={cnt_0,cnt_1,cnt_2};
int ret=1e9;
for(int cur=0;cur<3;cur++)
if(cur!=prv&&seen[cur]<pointer[cur])
{
int would=where[cur][seen[cur]+1],should=cnt_0+cnt_1+cnt_2+1;
int add=0;
for(int t=0;t<3;t++)
if(t!=cur)
add=add+max(0,seen[t]-cnt[t][would]);
//cout<<cnt_0<<" "<<cnt_1<<" "<<cnt_2<<" "<<would<<" "<<add<<" "<<should<<endl;
ret=min(ret,abs(would+add-should)+rec(cur,cnt_0+(cur==0),cnt_1+(cur==1),cnt_2+(cur==2)));
}
if(prv!=-1)dp[prv][cnt_0][cnt_1][cnt_2]=ret;
return ret;
}
int main()
{
memset(dp,-1,sizeof(dp));
scanf("%i",&n);
for(int i=1;i<=n;i++)
{
char c=getchar();
while(c!='R'&&c!='G'&&c!='Y')c=getchar();
if(c=='R')inp[i]=0;
if(c=='G')inp[i]=1;
if(c=='Y')inp[i]=2;
pointer[inp[i]]++;
where[inp[i]][pointer[inp[i]]]=i;
cnt[0][i]=cnt[0][i-1]+(inp[i]==0);
cnt[1][i]=cnt[1][i-1]+(inp[i]==1);
cnt[2][i]=cnt[2][i-1]+(inp[i]==2);
}
int MX=(n+1)/2;
if(pointer[0]>MX||pointer[1]>MX||pointer[2]>MX)
{
printf("-1\n");
return 0;
}
printf("%i\n",rec(-1,0,0,0));
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
47 | scanf("%i",&n);
| ~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
97072 KB |
Output is correct |
2 |
Correct |
65 ms |
97152 KB |
Output is correct |
3 |
Correct |
55 ms |
97144 KB |
Output is correct |
4 |
Correct |
55 ms |
97144 KB |
Output is correct |
5 |
Correct |
62 ms |
97144 KB |
Output is correct |
6 |
Correct |
56 ms |
97144 KB |
Output is correct |
7 |
Correct |
55 ms |
97144 KB |
Output is correct |
8 |
Correct |
56 ms |
97144 KB |
Output is correct |
9 |
Correct |
73 ms |
97144 KB |
Output is correct |
10 |
Correct |
55 ms |
97144 KB |
Output is correct |
11 |
Correct |
75 ms |
97144 KB |
Output is correct |
12 |
Correct |
56 ms |
97144 KB |
Output is correct |
13 |
Correct |
55 ms |
97144 KB |
Output is correct |
14 |
Correct |
56 ms |
97144 KB |
Output is correct |
15 |
Correct |
56 ms |
97144 KB |
Output is correct |
16 |
Correct |
59 ms |
97124 KB |
Output is correct |
17 |
Correct |
54 ms |
97052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
97072 KB |
Output is correct |
2 |
Correct |
65 ms |
97152 KB |
Output is correct |
3 |
Correct |
55 ms |
97144 KB |
Output is correct |
4 |
Correct |
55 ms |
97144 KB |
Output is correct |
5 |
Correct |
62 ms |
97144 KB |
Output is correct |
6 |
Correct |
56 ms |
97144 KB |
Output is correct |
7 |
Correct |
55 ms |
97144 KB |
Output is correct |
8 |
Correct |
56 ms |
97144 KB |
Output is correct |
9 |
Correct |
73 ms |
97144 KB |
Output is correct |
10 |
Correct |
55 ms |
97144 KB |
Output is correct |
11 |
Correct |
75 ms |
97144 KB |
Output is correct |
12 |
Correct |
56 ms |
97144 KB |
Output is correct |
13 |
Correct |
55 ms |
97144 KB |
Output is correct |
14 |
Correct |
56 ms |
97144 KB |
Output is correct |
15 |
Correct |
56 ms |
97144 KB |
Output is correct |
16 |
Correct |
59 ms |
97124 KB |
Output is correct |
17 |
Correct |
54 ms |
97052 KB |
Output is correct |
18 |
Correct |
55 ms |
97144 KB |
Output is correct |
19 |
Correct |
56 ms |
97144 KB |
Output is correct |
20 |
Correct |
58 ms |
97144 KB |
Output is correct |
21 |
Correct |
63 ms |
97144 KB |
Output is correct |
22 |
Correct |
57 ms |
97144 KB |
Output is correct |
23 |
Correct |
61 ms |
97144 KB |
Output is correct |
24 |
Correct |
66 ms |
97144 KB |
Output is correct |
25 |
Correct |
64 ms |
97144 KB |
Output is correct |
26 |
Correct |
62 ms |
97144 KB |
Output is correct |
27 |
Correct |
63 ms |
97144 KB |
Output is correct |
28 |
Correct |
60 ms |
97144 KB |
Output is correct |
29 |
Correct |
64 ms |
97104 KB |
Output is correct |
30 |
Correct |
62 ms |
97076 KB |
Output is correct |
31 |
Correct |
57 ms |
97036 KB |
Output is correct |
32 |
Correct |
55 ms |
97144 KB |
Output is correct |
33 |
Correct |
61 ms |
97144 KB |
Output is correct |
34 |
Correct |
54 ms |
97144 KB |
Output is correct |
35 |
Correct |
57 ms |
97144 KB |
Output is correct |
36 |
Correct |
66 ms |
97136 KB |
Output is correct |
37 |
Correct |
59 ms |
97144 KB |
Output is correct |
38 |
Correct |
65 ms |
97148 KB |
Output is correct |
39 |
Correct |
60 ms |
97040 KB |
Output is correct |
40 |
Correct |
54 ms |
97144 KB |
Output is correct |
41 |
Correct |
69 ms |
97148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
97144 KB |
Output is correct |
2 |
Correct |
60 ms |
97144 KB |
Output is correct |
3 |
Correct |
64 ms |
97144 KB |
Output is correct |
4 |
Correct |
58 ms |
97144 KB |
Output is correct |
5 |
Correct |
58 ms |
97144 KB |
Output is correct |
6 |
Correct |
55 ms |
97144 KB |
Output is correct |
7 |
Correct |
60 ms |
97144 KB |
Output is correct |
8 |
Correct |
58 ms |
97144 KB |
Output is correct |
9 |
Correct |
55 ms |
97144 KB |
Output is correct |
10 |
Correct |
60 ms |
97120 KB |
Output is correct |
11 |
Correct |
58 ms |
97144 KB |
Output is correct |
12 |
Correct |
61 ms |
97144 KB |
Output is correct |
13 |
Correct |
54 ms |
97144 KB |
Output is correct |
14 |
Correct |
60 ms |
97108 KB |
Output is correct |
15 |
Correct |
60 ms |
97144 KB |
Output is correct |
16 |
Correct |
54 ms |
97144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
97072 KB |
Output is correct |
2 |
Correct |
65 ms |
97152 KB |
Output is correct |
3 |
Correct |
55 ms |
97144 KB |
Output is correct |
4 |
Correct |
55 ms |
97144 KB |
Output is correct |
5 |
Correct |
62 ms |
97144 KB |
Output is correct |
6 |
Correct |
56 ms |
97144 KB |
Output is correct |
7 |
Correct |
55 ms |
97144 KB |
Output is correct |
8 |
Correct |
56 ms |
97144 KB |
Output is correct |
9 |
Correct |
73 ms |
97144 KB |
Output is correct |
10 |
Correct |
55 ms |
97144 KB |
Output is correct |
11 |
Correct |
75 ms |
97144 KB |
Output is correct |
12 |
Correct |
56 ms |
97144 KB |
Output is correct |
13 |
Correct |
55 ms |
97144 KB |
Output is correct |
14 |
Correct |
56 ms |
97144 KB |
Output is correct |
15 |
Correct |
56 ms |
97144 KB |
Output is correct |
16 |
Correct |
59 ms |
97124 KB |
Output is correct |
17 |
Correct |
54 ms |
97052 KB |
Output is correct |
18 |
Correct |
55 ms |
97144 KB |
Output is correct |
19 |
Correct |
56 ms |
97144 KB |
Output is correct |
20 |
Correct |
58 ms |
97144 KB |
Output is correct |
21 |
Correct |
63 ms |
97144 KB |
Output is correct |
22 |
Correct |
57 ms |
97144 KB |
Output is correct |
23 |
Correct |
61 ms |
97144 KB |
Output is correct |
24 |
Correct |
66 ms |
97144 KB |
Output is correct |
25 |
Correct |
64 ms |
97144 KB |
Output is correct |
26 |
Correct |
62 ms |
97144 KB |
Output is correct |
27 |
Correct |
63 ms |
97144 KB |
Output is correct |
28 |
Correct |
60 ms |
97144 KB |
Output is correct |
29 |
Correct |
64 ms |
97104 KB |
Output is correct |
30 |
Correct |
62 ms |
97076 KB |
Output is correct |
31 |
Correct |
57 ms |
97036 KB |
Output is correct |
32 |
Correct |
55 ms |
97144 KB |
Output is correct |
33 |
Correct |
61 ms |
97144 KB |
Output is correct |
34 |
Correct |
54 ms |
97144 KB |
Output is correct |
35 |
Correct |
57 ms |
97144 KB |
Output is correct |
36 |
Correct |
66 ms |
97136 KB |
Output is correct |
37 |
Correct |
59 ms |
97144 KB |
Output is correct |
38 |
Correct |
65 ms |
97148 KB |
Output is correct |
39 |
Correct |
60 ms |
97040 KB |
Output is correct |
40 |
Correct |
54 ms |
97144 KB |
Output is correct |
41 |
Correct |
69 ms |
97148 KB |
Output is correct |
42 |
Correct |
53 ms |
97144 KB |
Output is correct |
43 |
Correct |
60 ms |
97144 KB |
Output is correct |
44 |
Correct |
64 ms |
97144 KB |
Output is correct |
45 |
Correct |
58 ms |
97144 KB |
Output is correct |
46 |
Correct |
58 ms |
97144 KB |
Output is correct |
47 |
Correct |
55 ms |
97144 KB |
Output is correct |
48 |
Correct |
60 ms |
97144 KB |
Output is correct |
49 |
Correct |
58 ms |
97144 KB |
Output is correct |
50 |
Correct |
55 ms |
97144 KB |
Output is correct |
51 |
Correct |
60 ms |
97120 KB |
Output is correct |
52 |
Correct |
58 ms |
97144 KB |
Output is correct |
53 |
Correct |
61 ms |
97144 KB |
Output is correct |
54 |
Correct |
54 ms |
97144 KB |
Output is correct |
55 |
Correct |
60 ms |
97108 KB |
Output is correct |
56 |
Correct |
60 ms |
97144 KB |
Output is correct |
57 |
Correct |
54 ms |
97144 KB |
Output is correct |
58 |
Correct |
328 ms |
97272 KB |
Output is correct |
59 |
Correct |
230 ms |
97144 KB |
Output is correct |
60 |
Correct |
234 ms |
97144 KB |
Output is correct |
61 |
Correct |
256 ms |
97144 KB |
Output is correct |
62 |
Correct |
56 ms |
97148 KB |
Output is correct |
63 |
Correct |
60 ms |
97144 KB |
Output is correct |
64 |
Correct |
83 ms |
97144 KB |
Output is correct |
65 |
Correct |
125 ms |
97144 KB |
Output is correct |
66 |
Correct |
258 ms |
97144 KB |
Output is correct |
67 |
Correct |
215 ms |
97144 KB |
Output is correct |
68 |
Correct |
250 ms |
97144 KB |
Output is correct |
69 |
Correct |
234 ms |
97144 KB |
Output is correct |
70 |
Correct |
226 ms |
97144 KB |
Output is correct |
71 |
Correct |
218 ms |
97144 KB |
Output is correct |
72 |
Correct |
54 ms |
97144 KB |
Output is correct |
73 |
Correct |
59 ms |
97060 KB |
Output is correct |