Submission #508409

#TimeUsernameProblemLanguageResultExecution timeMemory
508409cig32Growing Vegetable is Fun 3 (JOI19_ho_t3)C++17
60 / 100
519 ms1048580 KiB
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count()); const int MAXN = 3e5 + 10; const int MOD = 1e9 + 7; #define int long long int rnd(int x, int y) { // random number generator int u= uniform_int_distribution<int>(x, y)(rng); return u; } void solve(int tc) { int N; cin >> N; string S; cin >> S; S = " " + S; int dp[N+1][N+1][N+1][3]; for(int i=0; i<=N; i++) { for(int j=0; j<=N; j++) { for(int k=0; k<=N; k++) { for(int l=0; l<3; l++) { dp[i][j][k][l] = 1e18; } } } } int before[N+1][3]; int ordinal[N+1][3]; for(int i=0; i<=N; i++) { for(int j=0; j<3; j++) { before[i][j] = 0; ordinal[i][j] = -1; } } string T = "RGY"; for(int i=1; i<=N; i++) { for(int j=0; j<3; j++) { before[i][j] = before[i-1][j] + (S[i-1] == T[j]); } } int counter[3] = {}; for(int i=1; i<=N; i++) { int id; for(int j=0; j<3; j++) { if(T[j] == S[i]) id = j; } ordinal[counter[id] + 1][id] = i; counter[id]++; } for(int i=0; i<3; i++) { dp[0][0][0][i] = 0; } for(int i=0; i<=N; i++) { for(int j=0; j<=N; j++) { if(i+j > N) continue; for(int k=0; k<=N-i-j; k++) { if(i+j+k == 0) continue; if(i > 0) { int pos = ordinal[i][0]; if(pos == -1) continue; pos += max(0ll, j - before[pos][1]) + max(0ll, k - before[pos][2]); int ma = min(dp[i-1][j][k][1], dp[i-1][j][k][2]); dp[i][j][k][0] = abs(ma + pos - (i+j+k)); } if(j > 0) { int pos = ordinal[j][1]; if(pos == -1) continue; pos += max(0ll, i - before[pos][0]) + max(0ll, k - before[pos][2]); int ma = min(dp[i][j-1][k][0], dp[i][j-1][k][2]); dp[i][j][k][1] = abs(ma + pos - (i+j+k)); } if(k > 0) { int pos = ordinal[k][2]; if(pos == -1) continue; pos += max(0ll, i - before[pos][0]) + max(0ll, j - before[pos][1]); int ma = min(dp[i][j][k-1][0], dp[i][j][k-1][1]); dp[i][j][k][2] = abs(ma + pos - (i+j+k)); } } } } int ans = 1e18; for(int i=0; i<=N; i++) { for(int j=0; j<=N; j++) { for(int k=0; k<=N; k++) { if(i+j+k == N) { for(int l=0; l<3; l++) { ans = min(ans, dp[i][j][k][l]); } } } } } cout << (ans == 1e18 ? -1 : ans) << "\n"; } int32_t main(){ ios::sync_with_stdio(0); cin.tie(0); int t = 1; //cin >> t; for(int i=1; i<=t; i++) solve(i); }

Compilation message (stderr)

joi2019_ho_t3.cpp: In function 'void solve(long long int)':
joi2019_ho_t3.cpp:50:23: warning: 'id' may be used uninitialized in this function [-Wmaybe-uninitialized]
   50 |     ordinal[counter[id] + 1][id] = i;
      |             ~~~~~~~~~~^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...