Submission #1154571

#TimeUsernameProblemLanguageResultExecution timeMemory
1154571NurislamGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++20
100 / 100
242 ms28872 KiB
#include <bits/stdc++.h> using namespace std; //#define int long long //#define all(x) x.begin(),x.end() //#define rall(x) x.rbegin(),x.rend() //#define ff first //#define ss second //#define pb push_back //template<class T, class U> inline bool chmin(T& a, const U& b) { if (a > b) { a = b; return true; } return false; } //template<class T, class U> inline bool chmax(T& a, const U& b) { if (a < b) { a = b; return true; } return false; } //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //#define rnd(l, r) uniform_int_distribution <int> (l, r)(rng) const int inf = 1e6, mod = 1e9 + 7; void solve(){ int n; cin >> n; string s; cin >> s; int x = 0, y = 0, z = 0; vector<int> a, b, c; for(int i = 0; i < n; i ++ ) { if(s[i] == 'R')x ++, a.push_back(i); if(s[i] == 'G')y ++, b.push_back(i); if(s[i] == 'Y')z ++, c.push_back(i); }; a.push_back(n); b.push_back(n); c.push_back(n); int dp[x+1][y+1][z+1][3]; memset(dp, inf, sizeof(dp)); for(int i = 0; i < 3; i++)dp[0][0][0][i] = 0; for(int i = 0; i <= x; i ++ ) { for(int j = 0; j <= y; j ++ ) { for(int k = 0; k <= z; k ++ ) { for(int la = 0; la < 3; la ++ ) { // i + 1; if(la != 0 && i < x) { int lwb = lower_bound(b.begin(), b.end(), a[i]) - b.begin(); int lwc = lower_bound(c.begin(), c.end(), a[i]) - c.begin(); int cost = max(0, lwb - j) + max(0, lwc - k); dp[i+1][j][k][0] = min(dp[i+1][j][k][0], cost + dp[i][j][k][la]); }; // j + 1; if(la != 1 && j < y) { int lwa = lower_bound(a.begin(), a.end(), b[j]) - a.begin(); int lwc = lower_bound(c.begin(), c.end(), b[j]) - c.begin(); int cost = max(0, lwa - i) + max(0, lwc - k); dp[i][j+1][k][1] = min(dp[i][j+1][k][1], cost + dp[i][j][k][la]); }; // k + 1; if(la != 2 && k < z) { int lwb = lower_bound(b.begin(), b.end(), c[k]) - b.begin(); int lwa = lower_bound(a.begin(), a.end(), c[k]) - a.begin(); int cost = max(0, lwb - j) + max(0, lwa - i); dp[i][j][k+1][2] = min(dp[i][j][k+1][2], cost + dp[i][j][k][la]); }; //cout << i << ' ' << j << ' ' << k << ' '<< la << ' ' << dp[i][j][k][la] << '\n'; }; }; }; }; int ans = inf; for(int i = 0; i < 3; i++) ans = min(ans, dp[x][y][z][i]); if(ans == inf)cout << -1 << '\n'; else cout << ans << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr); int tt = 1; //cin >> tt; while(tt--){ 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...