This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
6
RRRRRG
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i,n) for(int i=0;i<((int)n);i++)
#define pb emplace_back
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
out<<'('<<P.F<<','<<P.S<<')';
return out;
}
#define version 20190713
//}}}
const ll maxn=405;
const ll maxlg=__lg(maxn)+2;
const ll INF64=8000000000000000000LL;
const int INF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;
//const ll p=880301;
//const ll P=31;
ll mypow(ll a,ll b){
ll res=1LL;
while(b){
if(b&1) res=res*a%MOD;
a=a*a%MOD;
b>>=1;
}
return res;
}
int dp[maxn][maxn][maxn][3];
int cnt[3][3][maxn];
int idx[256];
int main(){
IOS;
int n;
string s;
cin>>n>>s;
idx['R']=0;
idx['G']=1;
idx['Y']=2;
int c[3]={0};
REP(i,n){
c[idx[s[i]]]++;
}
REP(i,3) REP(j,3) if(i!=j){
int tmp=0;
int pt=1;
REP(k,n){
if(idx[s[k]]==j) tmp++;
if(idx[s[k]]==i){
cnt[i][j][pt++]=tmp;
}
}
}
REP(i,c[0]+1) REP(j,c[1]+1) REP(k,c[2]+1){
if(!i&&!j&&!k){
REP(l,3) dp[i][j][k][l]=0;
continue;
}
{
if(i==0) dp[i][j][k][0]=INF;
else{
dp[i][j][k][0]=min(dp[i-1][j][k][1],dp[i-1][j][k][2]);
if(j>=cnt[0][1][i]) dp[i][j][k][0]+=j-cnt[0][1][i];
if(k>=cnt[0][2][i]) dp[i][j][k][0]+=k-cnt[0][2][i];
}
}
{
if(j==0) dp[i][j][k][1]=INF;
else{
dp[i][j][k][1]=min(dp[i][j-1][k][0],dp[i][j-1][k][2]);
if(i>=cnt[1][0][j]) dp[i][j][k][1]+=i-cnt[1][0][j];
if(k>=cnt[1][2][j]) dp[i][j][k][1]+=k-cnt[1][2][j];
}
}
{
if(k==0) dp[i][j][k][2]=INF;
else{
dp[i][j][k][2]=min(dp[i][j][k-1][0],dp[i][j][k-1][1]);
if(i>=cnt[2][0][k]) dp[i][j][k][2]+=i-cnt[2][0][k];
if(j>=cnt[2][1][k]) dp[i][j][k][2]+=j-cnt[2][1][k];
}
}
}
int tmp=*min_element(dp[c[0]][c[1]][c[2]],dp[c[0]][c[1]][c[2]]+3);
if(tmp>=INF) cout<<"-1\n";
else cout<<tmp<<'\n';
return 0;
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:67:19: warning: array subscript has type 'char' [-Wchar-subscripts]
c[idx[s[i]]]++;
^
joi2019_ho_t3.cpp:73:24: warning: array subscript has type 'char' [-Wchar-subscripts]
if(idx[s[k]]==j) tmp++;
^
joi2019_ho_t3.cpp:74:24: warning: array subscript has type 'char' [-Wchar-subscripts]
if(idx[s[k]]==i){
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |