//Original Code:
//#include <numeric>
//#include <iostream>
//#include <algorithm>
//#include <cmath>
//#include <stdio.h>
//#include <stdlib.h>
//#include <vector>
//#include <map>
//#include <queue>
//#include <set>
//#include <string>
//#include <string.h>
//#include <stack>
//#include <assert.h>
//#include <bitset>
//#include <time.h>
//#define Endl endl
//#define mp make_pair
//#define mt make_tuple
//#define ll long long
//#define ull unsigned long long
//#define pii pair<int,int>
//#define over(A) {cout<<A<<endl;exit(0);}
//#define all(A) A.begin(),A.end()
//#define quickcin ios_base::sync_with_stdio(false);
//const int mod=1000000007;
//const int gmod=3;
//const int inf=1039074182;
//const double eps=1e-9;
//const double pi=3.141592653589793238462643383279;
//const ll llinf=2LL*inf*inf;
//template <typename T1,typename T2> inline void chmin(T1 &x,T2 b) {if(b<x) x=b;}
//template <typename T1,typename T2> inline void chmax(T1 &x,T2 b) {if(b>x) x=b;}
//inline void chadd(int &x,int b) {x+=b-mod;x+=(x>>31 & mod);}
//template <typename T1,typename T2> inline void chadd(T1 &x,T2 b) {x+=b;if(x>=mod) x-=mod;}
//template <typename T1,typename T2> inline void chmul(T1 &x,T2 b) {x=1LL*x*b%mod;}
//template <typename T1,typename T2> inline void chmod(T1 &x,T2 b) {x%=b,x+=b;if(x>=b) x-=b;}
//template <typename T> inline T mabs(T x) {return (x<0?-x:x);}
//using namespace std;
//int n;
//int a[405];
//int cnt[3][405];
//char c[405];
//int dp[2][405][405][4];
//int pre[3][405][3];//???????,?????type???????
//int t[3];
//
//inline int calc(int tot,int a,int b,int c,int k)
//{
// t[0]=a;t[1]=b;t[2]=c;
// for(int i=0;i<3;i++) if(t[i]+(k==i)>cnt[i][n-1]) return inf;
// int cost=0;
// for(int i=0;i<3;i++)
// {
// cost+=max(0,t[i]-pre[k][t[k]+1][i]-(k==i));
// }
// return cost;
//}
//
//int main()
//{
// // freopen("input.txt","r",stdin);
// // freopen("b.in","r",stdin);
// // freopen("b.out","w",stdout);
// cin>>n;
// scanf("%s",c);
// for(int i=0;i<n;i++) a[i]=(c[i]=='R'?0:(c[i]=='G'?1:2));
// for(int i=0;i<n;i++)
// {
// for(int j=0;j<3;j++)
// {
// cnt[j][i]=(i?cnt[j][i-1]:0);
// if(a[i]==j) cnt[j][i]++;
// }
// for(int j=0;j<3;j++)
// {
// pre[a[i]][cnt[a[i]][i]][j]=t[j];
// }
// t[a[i]]=cnt[a[i]][i];
// }
// // for(int i=0;i<n;i++) cout<<a[i]<<' ';
// // cout<<endl;
// // return 0;
// dp[0][0][0][3]=0;
// for(int i=0;i<=n;i++)
// {
// int cur=i&1,nxt=cur^1;
// memset(dp[nxt],0x3f,sizeof(dp[nxt]));
// for(int a=0;a<=i;a++)
// {
// for(int b=0;b<=i-a;b++)
// {
// int c=i-a-b;
// for(int lst=0;lst<=3;lst++)
// {
// if(dp[cur][a][b][lst]>=inf) continue;
// for(int nlst=0;nlst<3;nlst++)
// {
// if(nlst==lst) continue;
// if(nlst==0) chmin(dp[nxt][a+1][b][nlst],dp[cur][a][b][lst]+calc(cur,a,b,c,0));
// if(nlst==1) chmin(dp[nxt][a][b+1][nlst],dp[cur][a][b][lst]+calc(i,a,b,c,1));
// if(nlst==2) chmin(dp[nxt][a][b][nlst],dp[cur][a][b][lst]+calc(i,a,b,c,2));
// }
// }
// }
// }
// }
// // cout<<dp[1][1][0][0]<<endl;
// // cout<<dp[2][1][1][1]<<endl;
// int res=inf;
// for(int lst=0;lst<3;lst++)
// {
// chmin(res,dp[n&1][cnt[0][n-1]][cnt[1][n-1]][lst]);
// }
// cout<<(res>=inf?-1:res)<<endl;
// return 0;
//}
//substituted with C++ Inliner
#include <numeric>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#include <assert.h>
#include <bitset>
#include <time.h>
#define Endl endl
#define mp make_pair
#define mt make_tuple
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
#define quickcin ios_base::sync_with_stdio(false);
const int mod=1000000007;
const int gmod=3;
const int inf=1039074182;
const double eps=1e-9;
const double pi=3.141592653589793238462643383279;
const ll llinf=2LL*inf*inf;
template <typename T1,typename T2> inline void chmin(T1 &x,T2 b) {if(b<x) x=b;}
template <typename T1,typename T2> inline void chmax(T1 &x,T2 b) {if(b>x) x=b;}
inline void chadd(int &x,int b) {x+=b-mod;x+=(x>>31 & mod);}
template <typename T1,typename T2> inline void chadd(T1 &x,T2 b) {x+=b;if(x>=mod) x-=mod;}
template <typename T1,typename T2> inline void chmul(T1 &x,T2 b) {x=1LL*x*b%mod;}
template <typename T1,typename T2> inline void chmod(T1 &x,T2 b) {x%=b,x+=b;if(x>=b) x-=b;}
template <typename T> inline T mabs(T x) {return (x<0?-x:x);}
using namespace std;
int n;
int a[405];
int cnt[3][405];
char c[405];
int dp[2][405][405][4];
int pre[3][405][3];//???????,?????type???????
int t[3];
inline int calc(int tot,int a,int b,int c,int k)
{
t[0]=a;t[1]=b;t[2]=c;
for(int i=0;i<3;i++) if(t[i]+(k==i)>cnt[i][n-1]) return inf;
int cost=0;
for(int i=0;i<3;i++)
{
cost+=max(0,t[i]-pre[k][t[k]+1][i]-(k==i));
}
return cost;
}
int main()
{
// // freopen("input.txt","r",stdin);
// freopen("b.in","r",stdin);
// freopen("b.out","w",stdout);
cin>>n;
scanf("%s",c);
for(int i=0;i<n;i++) a[i]=(c[i]=='R'?0:(c[i]=='G'?1:2));
for(int i=0;i<n;i++)
{
for(int j=0;j<3;j++)
{
cnt[j][i]=(i?cnt[j][i-1]:0);
if(a[i]==j) cnt[j][i]++;
}
for(int j=0;j<3;j++)
{
pre[a[i]][cnt[a[i]][i]][j]=t[j];
}
t[a[i]]=cnt[a[i]][i];
}
// for(int i=0;i<n;i++) cout<<a[i]<<' ';
// cout<<endl;
// return 0;
dp[0][0][0][3]=0;
for(int i=0;i<=n;i++)
{
int cur=i&1,nxt=cur^1;
memset(dp[nxt],0x3f,sizeof(dp[nxt]));
for(int a=0;a<=i;a++)
{
for(int b=0;b<=i-a;b++)
{
int c=i-a-b;
for(int lst=0;lst<=3;lst++)
{
if(dp[cur][a][b][lst]>=inf) continue;
for(int nlst=0;nlst<3;nlst++)
{
if(nlst==lst) continue;
if(nlst==0) chmin(dp[nxt][a+1][b][nlst],dp[cur][a][b][lst]+calc(cur,a,b,c,0));
if(nlst==1) chmin(dp[nxt][a][b+1][nlst],dp[cur][a][b][lst]+calc(i,a,b,c,1));
if(nlst==2) chmin(dp[nxt][a][b][nlst],dp[cur][a][b][lst]+calc(i,a,b,c,2));
}
}
}
}
}
// cout<<dp[1][1][0][0]<<endl;
// cout<<dp[2][1][1][1]<<endl;
int res=inf;
for(int lst=0;lst<3;lst++)
{
chmin(res,dp[n&1][cnt[0][n-1]][cnt[1][n-1]][lst]);
}
cout<<(res>=inf?-1:res)<<endl;
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:186:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
186 | scanf("%s",c);
| ~~~~~^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5324 KB |
Output is correct |
2 |
Correct |
3 ms |
5452 KB |
Output is correct |
3 |
Correct |
3 ms |
5452 KB |
Output is correct |
4 |
Correct |
4 ms |
5452 KB |
Output is correct |
5 |
Correct |
4 ms |
5452 KB |
Output is correct |
6 |
Correct |
4 ms |
5324 KB |
Output is correct |
7 |
Correct |
5 ms |
5324 KB |
Output is correct |
8 |
Correct |
5 ms |
5452 KB |
Output is correct |
9 |
Correct |
4 ms |
5324 KB |
Output is correct |
10 |
Correct |
5 ms |
5324 KB |
Output is correct |
11 |
Correct |
5 ms |
5324 KB |
Output is correct |
12 |
Correct |
5 ms |
5324 KB |
Output is correct |
13 |
Correct |
4 ms |
5368 KB |
Output is correct |
14 |
Correct |
4 ms |
5324 KB |
Output is correct |
15 |
Correct |
4 ms |
5452 KB |
Output is correct |
16 |
Correct |
5 ms |
5452 KB |
Output is correct |
17 |
Correct |
5 ms |
5324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5324 KB |
Output is correct |
2 |
Correct |
3 ms |
5452 KB |
Output is correct |
3 |
Correct |
3 ms |
5452 KB |
Output is correct |
4 |
Correct |
4 ms |
5452 KB |
Output is correct |
5 |
Correct |
4 ms |
5452 KB |
Output is correct |
6 |
Correct |
4 ms |
5324 KB |
Output is correct |
7 |
Correct |
5 ms |
5324 KB |
Output is correct |
8 |
Correct |
5 ms |
5452 KB |
Output is correct |
9 |
Correct |
4 ms |
5324 KB |
Output is correct |
10 |
Correct |
5 ms |
5324 KB |
Output is correct |
11 |
Correct |
5 ms |
5324 KB |
Output is correct |
12 |
Correct |
5 ms |
5324 KB |
Output is correct |
13 |
Correct |
4 ms |
5368 KB |
Output is correct |
14 |
Correct |
4 ms |
5324 KB |
Output is correct |
15 |
Correct |
4 ms |
5452 KB |
Output is correct |
16 |
Correct |
5 ms |
5452 KB |
Output is correct |
17 |
Correct |
5 ms |
5324 KB |
Output is correct |
18 |
Correct |
8 ms |
5324 KB |
Output is correct |
19 |
Correct |
9 ms |
5452 KB |
Output is correct |
20 |
Correct |
8 ms |
5452 KB |
Output is correct |
21 |
Correct |
9 ms |
5452 KB |
Output is correct |
22 |
Correct |
9 ms |
5324 KB |
Output is correct |
23 |
Correct |
8 ms |
5452 KB |
Output is correct |
24 |
Correct |
8 ms |
5444 KB |
Output is correct |
25 |
Correct |
8 ms |
5452 KB |
Output is correct |
26 |
Correct |
9 ms |
5444 KB |
Output is correct |
27 |
Correct |
10 ms |
5452 KB |
Output is correct |
28 |
Correct |
9 ms |
5452 KB |
Output is correct |
29 |
Correct |
8 ms |
5452 KB |
Output is correct |
30 |
Correct |
8 ms |
5452 KB |
Output is correct |
31 |
Correct |
12 ms |
5452 KB |
Output is correct |
32 |
Correct |
8 ms |
5324 KB |
Output is correct |
33 |
Correct |
8 ms |
5324 KB |
Output is correct |
34 |
Correct |
8 ms |
5324 KB |
Output is correct |
35 |
Correct |
8 ms |
5324 KB |
Output is correct |
36 |
Correct |
8 ms |
5448 KB |
Output is correct |
37 |
Correct |
8 ms |
5324 KB |
Output is correct |
38 |
Correct |
8 ms |
5440 KB |
Output is correct |
39 |
Correct |
9 ms |
5452 KB |
Output is correct |
40 |
Correct |
8 ms |
5452 KB |
Output is correct |
41 |
Correct |
10 ms |
5452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5452 KB |
Output is correct |
2 |
Correct |
90 ms |
5428 KB |
Output is correct |
3 |
Correct |
91 ms |
5324 KB |
Output is correct |
4 |
Correct |
81 ms |
5324 KB |
Output is correct |
5 |
Correct |
87 ms |
5428 KB |
Output is correct |
6 |
Correct |
85 ms |
5444 KB |
Output is correct |
7 |
Correct |
80 ms |
5324 KB |
Output is correct |
8 |
Correct |
89 ms |
5324 KB |
Output is correct |
9 |
Correct |
93 ms |
5324 KB |
Output is correct |
10 |
Correct |
83 ms |
5432 KB |
Output is correct |
11 |
Correct |
90 ms |
5428 KB |
Output is correct |
12 |
Correct |
29 ms |
5324 KB |
Output is correct |
13 |
Correct |
43 ms |
5452 KB |
Output is correct |
14 |
Correct |
61 ms |
5324 KB |
Output is correct |
15 |
Correct |
86 ms |
5324 KB |
Output is correct |
16 |
Correct |
81 ms |
5324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5324 KB |
Output is correct |
2 |
Correct |
3 ms |
5452 KB |
Output is correct |
3 |
Correct |
3 ms |
5452 KB |
Output is correct |
4 |
Correct |
4 ms |
5452 KB |
Output is correct |
5 |
Correct |
4 ms |
5452 KB |
Output is correct |
6 |
Correct |
4 ms |
5324 KB |
Output is correct |
7 |
Correct |
5 ms |
5324 KB |
Output is correct |
8 |
Correct |
5 ms |
5452 KB |
Output is correct |
9 |
Correct |
4 ms |
5324 KB |
Output is correct |
10 |
Correct |
5 ms |
5324 KB |
Output is correct |
11 |
Correct |
5 ms |
5324 KB |
Output is correct |
12 |
Correct |
5 ms |
5324 KB |
Output is correct |
13 |
Correct |
4 ms |
5368 KB |
Output is correct |
14 |
Correct |
4 ms |
5324 KB |
Output is correct |
15 |
Correct |
4 ms |
5452 KB |
Output is correct |
16 |
Correct |
5 ms |
5452 KB |
Output is correct |
17 |
Correct |
5 ms |
5324 KB |
Output is correct |
18 |
Correct |
8 ms |
5324 KB |
Output is correct |
19 |
Correct |
9 ms |
5452 KB |
Output is correct |
20 |
Correct |
8 ms |
5452 KB |
Output is correct |
21 |
Correct |
9 ms |
5452 KB |
Output is correct |
22 |
Correct |
9 ms |
5324 KB |
Output is correct |
23 |
Correct |
8 ms |
5452 KB |
Output is correct |
24 |
Correct |
8 ms |
5444 KB |
Output is correct |
25 |
Correct |
8 ms |
5452 KB |
Output is correct |
26 |
Correct |
9 ms |
5444 KB |
Output is correct |
27 |
Correct |
10 ms |
5452 KB |
Output is correct |
28 |
Correct |
9 ms |
5452 KB |
Output is correct |
29 |
Correct |
8 ms |
5452 KB |
Output is correct |
30 |
Correct |
8 ms |
5452 KB |
Output is correct |
31 |
Correct |
12 ms |
5452 KB |
Output is correct |
32 |
Correct |
8 ms |
5324 KB |
Output is correct |
33 |
Correct |
8 ms |
5324 KB |
Output is correct |
34 |
Correct |
8 ms |
5324 KB |
Output is correct |
35 |
Correct |
8 ms |
5324 KB |
Output is correct |
36 |
Correct |
8 ms |
5448 KB |
Output is correct |
37 |
Correct |
8 ms |
5324 KB |
Output is correct |
38 |
Correct |
8 ms |
5440 KB |
Output is correct |
39 |
Correct |
9 ms |
5452 KB |
Output is correct |
40 |
Correct |
8 ms |
5452 KB |
Output is correct |
41 |
Correct |
10 ms |
5452 KB |
Output is correct |
42 |
Correct |
3 ms |
5452 KB |
Output is correct |
43 |
Correct |
90 ms |
5428 KB |
Output is correct |
44 |
Correct |
91 ms |
5324 KB |
Output is correct |
45 |
Correct |
81 ms |
5324 KB |
Output is correct |
46 |
Correct |
87 ms |
5428 KB |
Output is correct |
47 |
Correct |
85 ms |
5444 KB |
Output is correct |
48 |
Correct |
80 ms |
5324 KB |
Output is correct |
49 |
Correct |
89 ms |
5324 KB |
Output is correct |
50 |
Correct |
93 ms |
5324 KB |
Output is correct |
51 |
Correct |
83 ms |
5432 KB |
Output is correct |
52 |
Correct |
90 ms |
5428 KB |
Output is correct |
53 |
Correct |
29 ms |
5324 KB |
Output is correct |
54 |
Correct |
43 ms |
5452 KB |
Output is correct |
55 |
Correct |
61 ms |
5324 KB |
Output is correct |
56 |
Correct |
86 ms |
5324 KB |
Output is correct |
57 |
Correct |
81 ms |
5324 KB |
Output is correct |
58 |
Correct |
131 ms |
5324 KB |
Output is correct |
59 |
Correct |
130 ms |
5428 KB |
Output is correct |
60 |
Correct |
124 ms |
5324 KB |
Output is correct |
61 |
Correct |
131 ms |
5424 KB |
Output is correct |
62 |
Correct |
83 ms |
5324 KB |
Output is correct |
63 |
Correct |
88 ms |
5324 KB |
Output is correct |
64 |
Correct |
101 ms |
5436 KB |
Output is correct |
65 |
Correct |
108 ms |
5324 KB |
Output is correct |
66 |
Correct |
119 ms |
5324 KB |
Output is correct |
67 |
Correct |
116 ms |
5324 KB |
Output is correct |
68 |
Correct |
131 ms |
5324 KB |
Output is correct |
69 |
Correct |
146 ms |
5324 KB |
Output is correct |
70 |
Correct |
125 ms |
5324 KB |
Output is correct |
71 |
Correct |
129 ms |
5424 KB |
Output is correct |
72 |
Correct |
82 ms |
5424 KB |
Output is correct |
73 |
Correct |
88 ms |
5324 KB |
Output is correct |