Submission #821467

#TimeUsernameProblemLanguageResultExecution timeMemory
821467hngwlogGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++14
100 / 100
138 ms41812 KiB
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define _size(x) (int)x.size() #define BIT(i, x) ((x >> i) & 1) #define MASK(n) ((1 << n) - 1) #define REP(i, n) for (int i = 0, _n = (n); i < _n; i++) #define FOR(i, a, b) for (int i = a, _b = (b); i <= _b; i++) #define FORD(i, a, b) for (int i = a, _b = (b); i >= _b; i--) #define FORB1(i, mask) for (int i = mask; i > 0; i ^= i & - i) #define FORB0(i, n, mask) for (int i = ((1 << n) - 1) ^ mask; i > 0; i ^= i & - i) #define FORALL(i, a) for (auto i: a) #define fastio ios_base::sync_with_stdio(0); cin.tie(0); const int inf = 1e9; int main() { fastio; int n; cin >> n; string s; cin >> s; vector<int> a(n + 1), cnt(3); vector<vector<int>> adj(3); s = ' ' + s; FOR(i, 1, n) { if (s[i] == 'R') a[i] = 0; else if (s[i] == 'G') a[i] = 1; else a[i] = 2; cnt[a[i]]++; adj[a[i]].push_back(i); } vector<vector<vector<vector<int>>>> g(3, vector<vector<vector<int>>>(3)); REP(i, 3) { REP(j, 3) { if (i == j) continue; g[i][j].assign(cnt[i] + 1, vector<int>(cnt[j] + 1)); REP(c, cnt[i] + 1) FOR(_c, 1, cnt[j]) { g[i][j][c][_c] = max((int)(lower_bound(adj[i].begin(), adj[i].end(), adj[j][_c - 1]) - adj[i].begin()) - c, 0); } } } vector<vector<vector<vector<int>>>> f(3, vector<vector<vector<int>>>(cnt[0] + 1, vector<vector<int>>(cnt[1] + 1, vector<int>(cnt[2] + 1, inf)))); REP(i, 3) f[i][0][0][0] = 0; REP(sum, n) { REP(cur, 3) { REP(i, min(sum, cnt[0]) + 1) REP(j, min(sum - i, cnt[1]) + 1) { int z = sum - i - j; if (z > cnt[2]) continue; if (cur != 0 && i + 1 <= cnt[0]) f[0][i + 1][j][z] = min(f[0][i + 1][j][z], f[cur][i][j][z] + g[1][0][j][i + 1] + g[2][0][z][i + 1]); if (cur != 1 && j + 1 <= cnt[1]) f[1][i][j + 1][z] = min(f[1][i][j + 1][z], f[cur][i][j][z] + g[0][1][i][j + 1] + g[2][1][z][j + 1]); if (cur != 2 && z + 1 <= cnt[2]) f[2][i][j][z + 1] = min(f[2][i][j][z + 1], f[cur][i][j][z] + g[0][2][i][z + 1] + g[1][2][j][z + 1]); } } } int ans = inf; REP(i, 3) ans = min(ans, f[i][cnt[0]][cnt[1]][cnt[2]]); cout << (ans == inf ? - 1 : ans) << '\n'; 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...