This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize ("O2,unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define SZ(x) ((int)(x).size())
const int inf=1000000010;
const ll INF=1000000000000001000LL;
const int mod=1000000007;
const int N=405;
int n, m, k, u, v, x, y, t, a, b, ans;
int dp[N][N][N][3], cnt[N][3];
vector<int> R, G, Y;
string S;
inline void upd(int &x, int y){ if (x>y) x=y;}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin>>n>>S;
for (int i=1; i<=n; i++){
memcpy(cnt[i], cnt[i-1], sizeof(cnt[i]));
if (S[i-1]=='R') R.pb(i), cnt[i][0]++;
if (S[i-1]=='G') G.pb(i), cnt[i][1]++;
if (S[i-1]=='Y') Y.pb(i), cnt[i][2]++;
}
memset(dp, 63, sizeof(dp));
dp[0][0][0][0]=dp[0][0][0][1]=dp[0][0][0][2]=0;
for (int r=0; r<=SZ(R); r++) for (int g=0; g<=SZ(G); g++) for (int y=0; y<=SZ(Y); y++){
if (r<SZ(R)){
int cost=max(0, cnt[R[r]][1]-g)+max(0, cnt[R[r]][2]-y);
upd(dp[r+1][g][y][0], min(dp[r][g][y][1], dp[r][g][y][2])+cost);
}
if (g<SZ(G)){
int cost=max(0, cnt[G[g]][0]-r)+max(0, cnt[G[g]][2]-y);
upd(dp[r][g+1][y][1], min(dp[r][g][y][0], dp[r][g][y][2])+cost);
}
if (y<SZ(Y)){
int cost=max(0, cnt[Y[y]][0]-r)+max(0, cnt[Y[y]][1]-g);
upd(dp[r][g][y+1][2], min(dp[r][g][y][0], dp[r][g][y][1])+cost);
}
}
ans=inf;
for (int i:{0, 1, 2}) upd(ans, dp[SZ(R)][SZ(G)][SZ(Y)][i]);
if (ans==inf) ans=-1;
cout<<ans<<"\n";
return 0;
}
# | 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... |