# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
896533 | Andrey | Growing Vegetable is Fun 3 (JOI19_ho_t3) | C++14 | 199 ms | 754848 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;
vector<int> wut[3];
int dp[400][401][401][3];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
string s;
cin >> s;
vector<int> haha(n);
for(int i = 0; i < n; i++) {
if(s[i] == 'R') {
haha[i] = 0;
}
else if(s[i] == 'G') {
haha[i] = 1;
}
else {
haha[i] = 2;
}
wut[haha[i]].push_back(i);
}
for(int i = 0; i < n; i++) {
for(int j = 0; j <= wut[0].size(); j++) {
for(int k = 0; k <= wut[1].size(); k++) {
dp[i][j][k][0] = 1e9;
dp[i][j][k][1] = 1e9;
dp[i][j][k][2] = 1e9;
if(j+k > i+1 || i+1-j-k > wut[2].size()) {
continue;
}
int b = i+1-j-k;
if(i > 0) {
if(j != 0) {
dp[i][j][k][0] = min(dp[i][j][k][0],min(dp[i-1][j-1][k][1],dp[i-1][j-1][k][2])+abs(i-wut[0][j-1]));
}
if(k != 0) {
dp[i][j][k][1] = min(dp[i][j][k][1],min(dp[i-1][j][k-1][0],dp[i-1][j][k-1][2])+abs(i-wut[1][k-1]));
}
if(b != 0) {
dp[i][j][k][2] = min(dp[i][j][k][2],min(dp[i-1][j][k][0],dp[i-1][j][k][1])+abs(i-wut[2][b-1]));
}
}
else {
if(j > 0) {
dp[i][j][k][0] = abs(i-wut[0][0]);
}
if(k > 0) {
dp[i][j][k][1] = abs(i-wut[1][0]);
}
if(b > 0) {
dp[i][j][k][2] = abs(i-wut[2][0]);
}
}
}
}
}
int ans = dp[n-1][wut[0].size()][wut[1].size()][0];
ans = min(ans,dp[n-1][wut[0].size()][wut[1].size()][1]);
ans = min(ans,dp[n-1][wut[0].size()][wut[1].size()][2]);
if(ans >= (int)1e9) {
cout << -1;
}
else {
cout << ans/2;
}
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... |