Submission #308809

#TimeUsernameProblemLanguageResultExecution timeMemory
308809HynDufGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++11
100 / 100
34 ms3200 KiB
#include <bits/stdc++.h> #define task "V" #define all(v) (v).begin(), (v).end() #define rep(i, l, r) for (int i = (l); i <= (r); ++i) #define Rep(i, r, l) for (int i = (r); i >= (l); --i) #define DB(X) { cerr << #X << " = " << (X) << '\n'; } #define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; } #define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; } #define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; } #define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';} #define SZ(x) ((int)(x).size()) #define pb push_back #define eb emplace_back #define pf push_front #define F first #define S second #define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a)); #define next ___next #define prev ___prev #define y1 ___y1 #define left ___left #define right ___right #define y0 ___y0 #define div ___div #define j0 ___j0 #define jn ___jn using ll = long long; using ld = long double; using ull = unsigned long long; using namespace std; typedef pair<int, int> ii; typedef pair<ii, int> iii; typedef vector<int> vi; typedef vector<ii> vii; typedef vector<ll> vl; const int N = 402, INF = 1e9; int n, dp[2][3][N][N], pre[3][N]; string s; map<char, int> mp; vi pos[3]; void upd(int &x, int y) {x = min(x, y);} int main() { #ifdef HynDuf freopen(task".in", "r", stdin); //freopen(task".out", "w", stdout); #else ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif cin >> n >> s; s = '0' + s; mp['R'] = 0; mp['G'] = 1; mp['Y'] = 2; pos[0].eb(0); pos[1].eb(0); pos[2].eb(0); rep(i, 1, n) { rep(j, 0, 2) pre[j][i] = pre[j][i - 1]; pre[mp[s[i]]][i]++; pos[mp[s[i]]].eb(i); } rep(i, 0, pre[0][n]) rep(j, 0, pre[1][n]) rep(t, 0, 2) dp[0][t][i][j] = INF; rep(t, 0, 2) dp[0][t][0][0] = 0; rep(i, 1, n) { int now = i & 1, lst = now ^ 1; rep(i1, 0, pre[0][n]) rep(j, 0, pre[1][n]) rep(t, 0, 2) dp[now][t][i1][j] = INF; rep(num0, 0, min(pre[0][n], (i + 1) >> 1)) rep(num1, 0, min({pre[1][n], i - num0, (i + 1) >> 1})) { int num2 = i - num0 - num1; if (num2 > pre[2][n]) continue; if (num0) { int cost0 = max(0, pre[1][pos[0][num0]] - num1) + max(0, pre[2][pos[0][num0]] - num2); upd(dp[now][0][num0][num1], dp[lst][1][num0 - 1][num1] + cost0); upd(dp[now][0][num0][num1], dp[lst][2][num0 - 1][num1] + cost0); //cerr << "dp[" << i << "][0][" << num0 << "][" << num1 << "] = " << dp[now][0][num0][num1] << '\n'; } if (num1) { int cost1 = max(0, pre[0][pos[1][num1]] - num0) + max(0, pre[2][pos[1][num1]] - num2); upd(dp[now][1][num0][num1], dp[lst][0][num0][num1 - 1] + cost1); upd(dp[now][1][num0][num1], dp[lst][2][num0][num1 - 1] + cost1); //cerr << "dp[" << i << "][1][" << num0 << "][" << num1 << "] = " << dp[now][1][num0][num1] << '\n'; } if (num2) { int cost2 = max(0, pre[1][pos[2][num2]] - num1) + max(0, pre[0][pos[2][num2]] - num0); upd(dp[now][2][num0][num1], dp[lst][1][num0][num1] + cost2); upd(dp[now][2][num0][num1], dp[lst][0][num0][num1] + cost2); //cerr << "dp[" << i << "][2][" << num0 << "][" << num1 << "] = " << dp[now][2][num0][num1] << '\n'; } } } int ans = 1e9; rep(j, 0, 2) ans = min(ans, dp[n & 1][j][pre[0][n]][pre[1][n]]); cout << (ans == 1e9 ? -1 : ans); 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...