# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
884516 | vjudge1 | Growing Vegetable is Fun 3 (JOI19_ho_t3) | C++17 | 460 ms | 1017544 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 ll long long
#define endl "\n"
#define all(aa) aa.begin(), aa.end()
const int INF=1e8;
int n;
vector<int> ind[3], cnt[3];
int dp[402][402][402][4];
int f(array<int, 3> x, int l){
if(x[0]+x[1]+x[2]==n) return 0;
int &cur=dp[x[0]][x[1]][x[2]][l];
if(cur!=-1) return cur;
cur=INF;
for(int i=0; i<3; i++) if(i!=l && x[i]!=ind[i].size()){
int o=0, s=x[0]+x[1]+x[2];
for(int j=0; j<3; j++) if(j!=i)
o+=max(0, x[j]-cnt[j][ind[i][x[i]]]);
x[i]++;
cur=min(cur, f(x, i)+o+ind[i][x[i]-1]-s);
x[i]--;
}
return cur;
}
int main(){
string s;
cin>>n>>s;
for(int i=0; i<3; i++) cnt[i].resize(n);
for(int i=0; i<n; i++){
if(i!=0){
for(int j=0; j<3; j++){
cnt[j][i]=cnt[j][i-1];
}
}
if(s[i]=='R'){
ind[0].push_back(i);
cnt[0][i]++;
}
else if(s[i]=='G'){
ind[1].push_back(i);
cnt[1][i]++;
}
else{
ind[2].push_back(i);
cnt[2][i]++;
}
}
memset(dp, -1, sizeof(dp));
int ans=f(array<int, 3>{0, 0, 0}, 3);
cout<<(ans>=INF ? -1:ans)<<endl;
}
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... |