Submission #961641

#TimeUsernameProblemLanguageResultExecution timeMemory
961641BhavayGoyalGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++14
100 / 100
261 ms134636 KiB
#include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define ll long long #define ld long double #define ar array #define vi vector<int> #define vii vector<vector<int>> #define pii pair<int, int> #define pb push_back #define all(x) x.begin(), x.end() #define f first #define s second #define endl "\n" const int MOD = 1e9+7; const int inf = 1e9; const ll linf = 1e18; const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1}; const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1}; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // -------------------------------------------------- Main Code -------------------------------------------------- /* YRGGRRGGYY dp[i][red][green][yellow][prevCol] = dp[i+1][] RYRGY */ const int N = 401; int n, arr[N], countPrev[4][N], r = 0, g = 0, y = 0;; vector<vector<vii>> dp; vi pos[4]; int sol(int red, int green, int yellow, int prev) { int idx = (red+green+yellow+1); if (idx == n+1) return 0; if (red > r || green > g || yellow > y) return inf; if (dp[red][green][yellow][prev] != -1) return dp[red][green][yellow][prev]; int ans = inf; for (int j = 1; j <= 3; j++) { if (j == prev) continue; int cost = inf; if (j == 1) { red++; if (red-1 < pos[j].size()) { int p = pos[j][red-1]; cost = max(0, countPrev[2][p] - green) + max(0, countPrev[3][p] - yellow); } } if (j == 2) { green++; if (green-1 < pos[j].size()) { int p = pos[j][green-1]; cost = max(0, countPrev[1][p] - red) + max(0, countPrev[3][p] - yellow); } } if (j == 3) { yellow++; if (yellow-1 < pos[j].size()) { int p = pos[j][yellow-1]; cost = max(0, countPrev[2][p] - green) + max(0, countPrev[1][p] - red); } } ans = min(ans, sol(red, green, yellow, j) + cost); if (j == 1) red--; if (j == 2) green--; if (j == 3) yellow--; } return dp[red][green][yellow][prev] = ans; } void sol() { cin >> n; string s; cin >> s; for (int i = 0; i < n; i++) { if (s[i] == 'R') arr[i+1] = 1, r++; if (s[i] == 'G') arr[i+1] = 2, g++; if (s[i] == 'Y') arr[i+1] = 3, y++; pos[arr[i+1]].pb(i+1); countPrev[arr[i+1]][i+1] = 1; } dp = vector<vector<vii>>(r+1, vector<vii>(g+1, vii(y+1, vi(4, -1)))); for (int j = 1; j <= 3; j++) { for (int i = 1; i <= n; i++) { countPrev[j][i] += countPrev[j][i-1]; } } int ans = sol(0, 0, 0, 0); cout << (ans==inf?-1:ans) << endl; } int main () { auto begin = std::chrono::high_resolution_clock::now(); ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; // cin >> t; for(int i = 1; i <= t; i++) { sol(); } auto end = std::chrono::high_resolution_clock::now(); auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin); cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; return 0; }

Compilation message (stderr)

joi2019_ho_t3.cpp: In function 'int sol(int, int, int, int)':
joi2019_ho_t3.cpp:60:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |             if (red-1 < pos[j].size()) {
      |                 ~~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:67:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |             if (green-1 < pos[j].size()) {
      |                 ~~~~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:74:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |             if (yellow-1 < pos[j].size()) {
      |                 ~~~~~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:80:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   80 |         if (j == 1) red--; if (j == 2) green--; if (j == 3) yellow--;
      |         ^~
joi2019_ho_t3.cpp:80:28: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   80 |         if (j == 1) red--; if (j == 2) green--; if (j == 3) yellow--;
      |                            ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...