Submission #1016121

#TimeUsernameProblemLanguageResultExecution timeMemory
1016121ByeWorldGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++14
100 / 100
271 ms166352 KiB
#include <bits/stdc++.h> #pragma GCC optimize("O3", "unroll-loops") #define ll long long #define int long long #define pb push_back #define fi first #define se second #define lf (id<<1) #define rg ((id<<1)|1) #define md ((l+r)/2) #define ld long double using namespace std; typedef pair<int,int> pii; typedef pair<pii, int> ipii; const int MAXN = 410; const int MAXA = 1e6+10; const int INF = 1e9+10; // const int INF2 = 1e18+10; const int LOG = 19; const int MOD = 1e9+7; const int SQRT = 450; const vector<int> NOL = {}; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const vector<int> dx = {1, -1, 0, 0}; const vector<int> dy = {0, 0, 1, -1}; void chmn(int &a, int b){ a = min(a, b); } void chmx(int &a, int b){ a = max(a, b); } int n; string s; int a[MAXN], siz[3]; int dp[MAXN][MAXN][MAXN][3]; vector <int> vec[3]; signed main(){ cin >> n; cin >> s; s = '.'+s; vec[0].pb(-1); vec[1].pb(-1); vec[2].pb(-1); for(int i=1; i<=n; i++){ if(s[i]=='R') a[i] = 0; else if(s[i]=='G') a[i] = 1; else a[i] = 2; // 'Y' vec[a[i]].pb(i); } for(int i=0; i<=2; i++) siz[i] = vec[i].size()-1; for(int i=0; i<=siz[0]; i++) for(int j=0; j<=siz[1]; j++) for(int k=0; k<=siz[2]; k++) for(int p=0; p<=2; p++) dp[i][j][k][p] = INF; for(int i=0; i<=2; i++) dp[0][0][0][i] = 0; for(int idx=1; idx<=n; idx++){ for(int x=0; x<=idx; x++){ for(int y=0; y<=idx-x; y++){ int z = idx-x-y; if(x>siz[0] || y>siz[1] || z>siz[2]) continue; int cost = INF; if(x!=0){ // ambil X int id = vec[0][x]; int yi = lower_bound(vec[1].begin(), vec[1].end(), id) - vec[1].begin() - 1; int zi = lower_bound(vec[2].begin(), vec[2].end(), id) - vec[2].begin() - 1; id += y-min(y,yi); id += z-min(z,zi); cost = abs(x+y+z - id); chmn(dp[x][y][z][0], min(dp[x-1][y][z][1], dp[x-1][y][z][2])+cost); } if(y!=0){ int id = vec[1][y]; int xi = lower_bound(vec[0].begin(), vec[0].end(), id) - vec[0].begin() - 1; int zi = lower_bound(vec[2].begin(), vec[2].end(), id) - vec[2].begin() - 1; id += x-min(x,xi); id += z-min(z,zi); cost = abs(x+y+z - id); chmn(dp[x][y][z][1], min(dp[x][y-1][z][0], dp[x][y-1][z][2])+cost); } if(z!=0){ int id = vec[2][z]; int xi = lower_bound(vec[0].begin(), vec[0].end(), id) - vec[0].begin() - 1; int yi = lower_bound(vec[1].begin(), vec[1].end(), id) - vec[1].begin() - 1; id += x-min(x,xi); id += y-min(y,yi); cost = abs(x+y+z - id); chmn(dp[x][y][z][2], min(dp[x][y][z-1][0], dp[x][y][z-1][1])+cost); } // cout << x << ' ' << y << ' ' << z << " xyz\n"; // for(int i=0; i<3; i++){ // cout << i << ' ' << dp[x][y][z][i] << " dp\n"; // } } } } int ANS = min(min(dp[siz[0]][siz[1]][siz[2]][0], dp[siz[0]][siz[1]][siz[2]][1]), dp[siz[0]][siz[1]][siz[2]][2]); cout << (ANS>=INF ? -1 : ANS) << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...