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>
using namespace std;
#define int long long
using vi = vector<int>;
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl '\n'
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
template <class T> istream &operator>>(istream& in, vector<T> &v) {for (auto& i : v) in >> i; return in;}
#ifdef LOCAL
#include "../../../lib/debug.h"
#else
#define dbg(...)
#endif
const int N=405,oo=1e18;
int trans[3][N][3];
int cnt[3];
map<char,int>m;
void ckmin(int&a,int b) {a=min(a,b);}
signed main() {
ios::sync_with_stdio(0);cin.tie(0);m['R']=0;m['G']=1;m['Y']=2;
int n;cin>>n;string s;cin>>s;
FOR(i,0,n)cnt[m[s[i]]]++;
vi y={0,0,0};
FOR(i,0,n){
y[m[s[i]]]++;
FOR(j,0,3)trans[m[s[i]]][y[m[s[i]]]][j]=y[j];
}
int dp[cnt[0]+1][cnt[1]+1][cnt[2]+1][3];FOR(i,0,cnt[0]+1)FOR(j,0,cnt[1]+1)FOR(k,0,cnt[2]+1)FOR(l,0,3)dp[i][j][k][l]=oo;FOR(i,0,3)dp[0][0][0][i]=1;
FOR(i,0,cnt[0]+1)FOR(j,0,cnt[1]+1)FOR(k,0,cnt[2]+1)FOR(last,0,3)FOR(next,0,3)if(last!=next){
vi x={i,j,k};if(x[next]==cnt[next])continue;
int nxt=dp[i][j][k][last];
x[next]++;
FOR(p,0,3)nxt+=max(0ll,trans[next][x[next]][p]-x[p]);
ckmin(dp[x[0]][x[1]][x[2]][next],nxt);
}
int ans=min({dp[cnt[0]][cnt[1]][cnt[2]][0], dp[cnt[0]][cnt[1]][cnt[2]][1], dp[cnt[0]][cnt[1]][cnt[2]][2]});
if(ans==oo)cout<<-1<<endl;
else cout<<ans-1<<endl;
}
# | 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... |