# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
217298 | kshitij_sodani | Growing Vegetable is Fun 3 (JOI19_ho_t3) | C++17 | 512 ms | 755512 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 <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long int llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl "\n"
llo mod=1000000007;
int n;
int it[401];
int dp[401][401][401][3];
vector<int> aa;
vector<int> bb;
vector<int> cc;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
char ss;
int co=0;
int co2=0;
for(int i=0;i<n;i++){
for(int j=0;j<=n;j++){
for(int k=0;k<=n;k++){
for(int l=0;l<3;l++){
dp[i][j][k][l]=400000;
}
}
}
}
for(int i=0;i<n;i++){
cin>>ss;
if(ss=='R'){
co+=1;
aa.pb(i);
}
else if(ss=='G'){
it[i]=1;
co2+=1;
bb.pb(i);
}
else{
cc.pb(i);
}
}
if(co>(n+1)/2 or co2>(n+1)/2 or (n-co-co2)>(n+1)/2){
cout<<-1<<endl;
return 0;
}
for(int i=0;i<n;i++){
for(int j=0;j<=n;j++){
for(int k=0;k<=n;k++){
if((i+1)-j-k>(i+2)/2 or j>(i+2)/2 or k>(i+2)/2 or j+k>i+1){
continue;
}
// cout<<i+1<<" "<<j<<" "<<k<<endl;
if(j>0 and j<=aa.size()){
if(i>0){
dp[i][j][k][0]=abs(i-aa[j-1])+min(dp[i-1][j-1][k][1],dp[i-1][j-1][k][2]);
}
else{
dp[i][j][k][0]=abs(i-aa[j-1]);
}
}
if(k>0 and k<=bb.size()){
if(i>0){
dp[i][j][k][1]=abs(i-bb[k-1])+min(dp[i-1][j][k-1][0],dp[i-1][j][k-1][2]);
}
else{
dp[i][j][k][1]=abs(i-bb[k-1]);
}
}
if(j+k<i+1 and i+1-j-k<=cc.size()){
if(i>0){
dp[i][j][k][2]=abs(cc[i-j-k]-i)+min(dp[i-1][j][k][0],dp[i-1][j][k][1]);
}
else{
dp[i][j][k][2]=abs(cc[i-j-k]-i);
}
}
// cout<<dp[i][j][k][0]<<","<<dp[i][j][k][1]<<","<<dp[i][j][k][2]<<endl;
}
}
}
int mi=400000;
for(int j=0;j<=3;j++){
mi=min(mi,dp[n-1][aa.size()][bb.size()][j]);
}
cout<<mi/2<<endl;
return 0;
}
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... |