# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
701384 | Baytoro | Growing Vegetable is Fun 3 (JOI19_ho_t3) | C++17 | 63 ms | 163048 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 ios ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fr first
#define sc second
#define endl '\n'
//#define int long long
#define ll long long
void fopn(string name){
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
const ll INF=1e18,mod=1e9+7;
const int N=405;
vector<int> r,g,y;
int p[N][3],dp[N][N][N][3];
void solve(){
int n;cin>>n;
string s;cin>>s;
for(int i=0;i<n;i++){
if(s[i]=='R') r.pb(i+1);
if(s[i]=='G') g.pb(i+1);
if(s[i]=='Y') y.pb(i+1);
p[i+1][0]=r.size();
p[i+1][1]=g.size();
p[i+1][2]=y.size();
}
for(int i=0;i<=(int)r.size();i++){
for(int j=0;j<=(int)g.size();j++){
for(int k=0;k<=(int)y.size();k++){
if(!i && !j && !k) continue;
for(int l=0;l<3;l++) dp[i][j][k][l]=mod;
int sum=i+j+k;
if(i) dp[i][j][k][0]=min(dp[i-1][j][k][1],dp[i-1][j][k][2])+((r[i-1]+max(0,j-p[r[i-1]][1])+max(0,k-p[r[i-1]][2]))-sum);
if(j) dp[i][j][k][1]=min(dp[i][j-1][k][0],dp[i][j-1][k][2])+((g[j-1]+max(0,i-p[g[j-1]][0])+max(0,k-p[g[j-1]][2]))-sum);
if(k) dp[i][j][k][2]=min(dp[i][j][k-1][0],dp[i][j][k-1][1])+((y[k-1]+max(0,i-p[y[k-1]][0])+max(0,j-p[y[k-1]][1]))-sum);
}
}
}
int ans=*min_element(dp[r.size()][g.size()][y.size()],dp[r.size()][g.size()][y.size()]+3);
if(ans>=mod) ans=-1;
cout<<ans;
}
main(){
ios;
int T=1;
//cin>>T;
while(T--){
solve();
}
}
Compilation message (stderr)
# | 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... |