# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
914354 | Keys | Growing Vegetable is Fun 3 (JOI19_ho_t3) | C++14 | 0 ms | 0 KiB |
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 int64_t
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 dp[N][N][N][3];
int p[3][N];
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]]]++;
p[m[s[i]]][y[m[s[i]]]]=i;
}
// FOR(i,0,3){FOR(j,0,n)cout<<p[i][j]<<' ';cout<<endl;}
FOR(i,0,N)FOR(j,0,N)FOR(k,0,N)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;
x[next]++;
ckmin(dp[x[0]][x[1]][x[2]][next],dp[i][j][k][last]+max(0ll,p[next][x[next]]-(i+j+k)));
}
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;
}