Submission #783204

#TimeUsernameProblemLanguageResultExecution timeMemory
783204borisAngelovGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++17
60 / 100
558 ms766580 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 405; const int inf = 1e9; int n; string s; vector<int> pos[3]; int preffix[maxn][3]; int dp[maxn][maxn][maxn][3]; int decode(char c) { if (c == 'R') { return 0; } if (c == 'G') { return 1; } return 2; } void fastIO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int main() { fastIO(); cin >> n >> s; s = '#' + s; for (int i = 1; i <= n; ++i) { int curr = decode(s[i]); for (int j = 0; j < 3; ++j) { preffix[i][j] = preffix[i - 1][j]; } ++preffix[i][curr]; pos[curr].push_back(i); } for (int i = 0; i <= n; ++i) { for (int j = 0; j <= n; ++j) { for (int k = 0; k <= n; ++k) { for (int p = 0; p <= 2; ++p) { dp[i][j][k][p] = inf; } } } } dp[0][0][0][0] = 0; dp[0][0][0][1] = 0; dp[0][0][0][2] = 0; int cnt0 = preffix[n][0]; int cnt1 = preffix[n][1]; int cnt2 = preffix[n][2]; if ((1LL * cnt0) * (1LL * cnt1) * (1LL * cnt2) > 1e7) { cout << "Error" << endl; return 0; } for (int i = 0; i <= cnt0; ++i) { for (int j = 0; j <= cnt1; ++j) { for (int k = 0; k <= cnt2; ++k) { if (i != 0) { int curr_min = min(dp[i - 1][j][k][1], dp[i - 1][j][k][2]); int cost = max(0, preffix[pos[0][i - 1]][1] - j) + max(0, preffix[pos[0][i - 1]][2] - k); dp[i][j][k][0] = min(dp[i][j][k][0], curr_min + cost); } if (j != 0) { int curr_min = min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]); int cost = max(0, preffix[pos[1][j - 1]][0] - i) + max(0, preffix[pos[1][j - 1]][2] - k); dp[i][j][k][1] = min(dp[i][j][k][1], curr_min + cost); } if (k != 0) { int curr_min = min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]); int cost = max(0, preffix[pos[2][k - 1]][0] - i) + max(0, preffix[pos[2][k - 1]][1] - j); dp[i][j][k][2] = min(dp[i][j][k][2], curr_min + cost); } } } } int ans = min(dp[cnt0][cnt1][cnt2][0], min(dp[cnt0][cnt1][cnt2][1], dp[cnt0][cnt1][cnt2][2])); if (ans == inf) { cout << -1 << endl; } else { cout << ans << endl; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...