Submission #927544

#TimeUsernameProblemLanguageResultExecution timeMemory
927544Gromp15Growing Vegetable is Fun 3 (JOI19_ho_t3)C++17
0 / 100
1016 ms757556 KiB
#include <bits/stdc++.h> #define ll long long #define ar array #define db double #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define rint(l, r) uniform_int_distribution<int>(l, r)(rng) template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } const int INF = 1e9; void test_case() { int n; string s; cin >> n >> s; vector<int> a(n); for (int i = 0; i < n; i++) { a[i] = (s[i] == 'R' ? 0 : s[i] == 'G' ? 1 : 2); } ar<int, 3> on{}; int where[3][n+1]; memset(where, -1, sizeof(where)); for (int i = 0; i < n; i++) { where[a[i]][on[a[i]]++] = i; } int dp[n+1][n+1][n+1][3]; fill(&dp[0][0][0][0], &dp[0][0][0][0] + 3*(n+1)*(n+1)*(n+1), INF); memset(dp[0][0][0], 0, sizeof(dp[0][0][0])); for (int s = 0; s < n; s++) { for (int i = 0; i <= s; i++) { for (int j = 0; i+j <= s; j++) { ar<int, 3> cur{i, j, s-i-j}; for (int k = 0; k < 3; k++) { for (int nw = 0; nw < 3; nw++) if (nw != k) { int pos = where[nw][cur[nw]]; if (~pos) { ckmin(dp[i+(nw==0)][j+(nw==1)][s-i-j+(nw==2)][nw], dp[i][j][s-i-j][k] + abs(pos - s)); } } } } } } int ans = INF; for (int i = 0; i <= n; i++) for (int j = 0; i+j <= n; j++) { for (int k = 0; k < 3; k++) { ckmin(ans, dp[i][j][n-i-j][k]); } } cout << (ans == INF ? -1 : ans / 2) << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); int t = 1; // cin >> t; while (t--) test_case(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...