#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[i][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:66:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | scanf("%s",c);
| ~~~~~^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5324 KB |
Output is correct |
2 |
Correct |
3 ms |
5392 KB |
Output is correct |
3 |
Runtime error |
9 ms |
10820 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5324 KB |
Output is correct |
2 |
Correct |
3 ms |
5392 KB |
Output is correct |
3 |
Runtime error |
9 ms |
10820 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
9 ms |
10828 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5324 KB |
Output is correct |
2 |
Correct |
3 ms |
5392 KB |
Output is correct |
3 |
Runtime error |
9 ms |
10820 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |