Submission #765572

#TimeUsernameProblemLanguageResultExecution timeMemory
765572NK_Growing Vegetable is Fun 3 (JOI19_ho_t3)C++17
100 / 100
268 ms2712 KiB
// Success consists of going from failure to failure without loss of enthusiasm #include <bits/stdc++.h> using namespace std; #define nl '\n' #define pb push_back #define sz(x) int(x.size()) using str = string; template<class T> using V = vector<T>; const int INF = int(1e9) + 10; int main() { cin.tie(0)->sync_with_stdio(0); int N; cin >> N; str S; cin >> S; V<V<int>> oc(3); for(int i = 0; i < N; i++) { int t = (S[i] == 'R' ? 0 : (S[i] == 'G' ? 1 : 2)); oc[t].pb(i); } sort(begin(oc), end(oc), [&](const V<int> &a, const V<int> &b) { return size(a) < size(b); }); int A = sz(oc[0]), B = sz(oc[1]), C = N - A - B; V<V<V<int>>> COST(9); for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++) if (i != j) { COST[3 * i + j] = V<V<int>>(sz(oc[i]), V<int>(sz(oc[j]) + 1)); for(int x = 0; x < sz(oc[i]); x++) for(int y = 0; y <= sz(oc[j]); y++) { COST[3 * i + j][x][y] = (lower_bound(begin(oc[j]) + y, end(oc[j]), oc[i][x]) - (begin(oc[j]) + y)); // cout << 3 * i + j << " " << x << " " << y << endl; // cout << COST[3 * i + j][x][y] << endl; } } V<V<V<int>>> dp(A + 1, V<V<int>>(B + 1, V<int>(3, INF))); dp[0][0] = {0, 0, 0}; for(int i = 0; i < N; i++) { V<V<V<int>>> ndp(A + 1, V<V<int>>(B + 1, V<int>(3, INF))); for(int a = 0; a <= A; a++) for(int b = 0; b <= B; b++) for(int lst = 0; lst < 3; lst++) { if (dp[a][b][lst] == INF) continue; int c = i - a - b; if (c < 0) continue; // cout << i << " " << a << " " << b << " " << c << " " << lst << endl; // cout << dp[a][b][lst] << endl; // cout << endl; if (a + 1 <= A && lst != 0) ndp[a+1][b][0] = min(ndp[a+1][b][0], dp[a][b][lst] + COST[1][a][b] + COST[2][a][c]); // cout << 1 << endl; if (b + 1 <= B && lst != 1) ndp[a][b+1][1] = min(ndp[a][b+1][1], dp[a][b][lst] + COST[3][b][a] + COST[5][b][c]); // cout << 1 << endl; if (c + 1 <= C && lst != 2) ndp[a][b][2] = min(ndp[a][b][2], dp[a][b][lst] + COST[6][c][a] + COST[7][c][b]); } // cout << endl; dp.swap(ndp); } int ans = min({dp[A][B][0], dp[A][B][1], dp[A][B][2]}); cout << (ans == INF ? -1 : ans) << nl; 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...