Submission #747822

#TimeUsernameProblemLanguageResultExecution timeMemory
747822tch1cherinGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++17
75 / 100
1008 ms133200 KiB
#pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #include <bits/stdc++.h> using namespace std; void solve() { int n; string s; cin >> n >> s; vector<int> pos[3]; for (int i = 0; i < n; i++) { if (s[i] == 'R') { pos[0].push_back(i); } else if (s[i] == 'G') { pos[1].push_back(i); } else { pos[2].push_back(i); } } int R = pos[0].size(), G = pos[1].size(), Y = pos[2].size(); vector dp(R + 1, vector(G + 1, vector(Y + 1, vector<int>(4, 1e9)))); dp[0][0][0][3] = 0; for (int i = 0; i < n; i++) { for (int r = 0; r <= R && r <= i; r++) { for (int g = 0; g <= G && r + g <= i; g++) { int y = i - r - g; if (y > Y) { continue; } for (int l = 0; l < 4; l++) { if (l != 0 && r < R) { int a = lower_bound(pos[1].begin(), pos[1].end(), pos[0][r]) - pos[1].begin(); int b = lower_bound(pos[2].begin(), pos[2].end(), pos[0][r]) - pos[2].begin(); dp[r + 1][g][y][0] = min(dp[r + 1][g][y][0], dp[r][g][y][l] + max(0, g - a) + max(0, y - b)); } if (l != 1 && g < G) { int a = lower_bound(pos[0].begin(), pos[0].end(), pos[1][g]) - pos[0].begin(); int b = lower_bound(pos[2].begin(), pos[2].end(), pos[1][g]) - pos[2].begin(); dp[r][g + 1][y][1] = min(dp[r][g + 1][y][1], dp[r][g][y][l] + max(0, r - a) + max(0, y - b)); } if (l != 2 && y < Y) { int a = lower_bound(pos[0].begin(), pos[0].end(), pos[2][y]) - pos[0].begin(); int b = lower_bound(pos[1].begin(), pos[1].end(), pos[2][y]) - pos[1].begin(); dp[r][g][y + 1][2] = min(dp[r][g][y + 1][2], dp[r][g][y][l] + max(0, r - a) + max(0, g - b)); } } } } } int ans = min({dp[R][G][Y][0], dp[R][G][Y][1], dp[R][G][Y][2]}); cout << (ans == 1e9 ? -1 : ans) << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...