Submission #386886

#TimeUsernameProblemLanguageResultExecution timeMemory
386886cpp219Growing Vegetable is Fun 3 (JOI19_ho_t3)C++14
15 / 100
4 ms3308 KiB
#pragma GCC optimization "Ofast" #pragma GCC optimization "unroll-loop" #pragma GCC target ("avx2") #include <bits/stdc++.h> #define ll int #define ld long double #define fs first #define sc second using namespace std; const ll N = 400 + 6; const ll Log2 = 19; const ll inf = 1e9 + 7; typedef pair<ll,ll> LL; ll n,cntR,cntG,cntY,posR[N],posG[N],posY[N]; string s; int main(){ ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); #define task "tst" if (fopen(task".inp", "r")){ freopen(task".inp", "r", stdin); //freopen(task".out", "w", stdout); } cin>>n>>s; s = " " + s; for (ll i = 1;i <= n;i++){ if (s[i] == 'R') cntR++,posR[cntR] = i; if (s[i] == 'G') cntG++,posG[cntG] = i; if (s[i] == 'Y') cntY++,posY[cntY] = i; } ll lim = ceil((ld)n/2); if (cntR > lim||cntG > lim||cntY > lim){ cout<<-1; return 0; } ll dp[cntR + 6][cntG + 6][cntY + 6][3],ans; dp[cntR][cntG][cntY][0] = dp[cntR][cntG][cntY][1] = dp[cntR][cntG][cntY][2] = 0; for (ll i = cntR;i >= 0;i--){ for (ll j = cntG;j >= 0;j--){ for (ll l = cntY;l >= 0;l--){ if (i == cntR && j == cntG && l == cntY) continue; ll now = i + j + l + 1; for (ll k = 0;k < 3;k++){ ll p = inf; if (k == 0 && i){ if (j + 1 <= cntG) p = min(p,dp[i][j + 1][l][1] + max(0,posG[j + 1] - now)); if (l + 1 <= cntY) p = min(p,dp[i][j][l + 1][2] + max(0,posY[l + 1] - now)); } if (k == 1 && j){ if (i + 1 <= cntR) p = min(p,dp[i + 1][j][l][0] + max(0,posR[i + 1] - now)); if (l + 1 <= cntY) p = min(p,dp[i][j][l + 1][2] + max(0,posY[l + 1] - now)); } if (k == 2 && l){ if (i + 1 <= cntR) p = min(p,dp[i + 1][j][l][0] + max(0,posR[i + 1] - now)); if (j + 1 <= cntG) p = min(p,dp[i][j + 1][l][1] + max(0,posG[j + 1] - now)); } dp[i][j][l][k] = p; } } } } //cout<<cntR; return 0; //cout<<dp[1][1][0][1]; return 0; ans = inf; if (cntR) ans = min(ans,dp[1][0][0][0] + posR[1] - 1); if (cntG) ans = min(ans,dp[0][1][0][1] + posG[1] - 1); if (cntY) ans = min(ans,dp[0][0][1][2] + posY[1] - 1); cout<<ans; }

Compilation message (stderr)

joi2019_ho_t3.cpp:1: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    1 | #pragma GCC optimization "Ofast"
      | 
joi2019_ho_t3.cpp:2: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    2 | #pragma GCC optimization "unroll-loop"
      | 
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:24:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   24 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...