제출 #376975

#제출 시각아이디문제언어결과실행 시간메모리
376975peijarGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++17
60 / 100
557 ms1048580 KiB
#include <bits/stdc++.h> #define int long long using namespace std; const int MAXN = 401; const int INF = 1e18; int dp[MAXN][MAXN][MAXN][4]; // (last, nbRouges, nbVerts, nbJaunes) signed main(void) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int nbFleurs; string fleurs; cin >> nbFleurs >> fleurs; vector<vector<int>> positions(3); for (int i(0); i < nbFleurs; ++i) { if (fleurs[i] == 'R') positions[0].push_back(i); else if (fleurs[i] == 'G') positions[1].push_back(i); else positions[2].push_back(i); } for (int i(0); i < 3; ++i) positions[i].push_back(nbFleurs); for (int nbRouge(0); nbRouge <= nbFleurs; ++nbRouge) for (int nbVerts(0); nbVerts <= nbFleurs; ++nbVerts) for (int nbJaunes(0); nbJaunes <= nbFleurs; ++nbJaunes) for (int dernier(0); dernier < 4; ++dernier) dp[nbRouge][nbVerts][nbJaunes][dernier]= INF; int rep = INF; int totRouges = (int)positions[0].size() - 1; int totVerts = (int)positions[1].size() - 1; int totJaunes = (int)positions[2].size() - 1; assert(totRouges >= 0 and totVerts >= 0 and totJaunes >= 0); dp[0][0][0][3] = 0; for (int nbRouge(0); nbRouge <= totRouges; ++nbRouge) for (int nbVerts(0); nbVerts <= totVerts; ++nbVerts) for (int nbJaunes(0); nbJaunes <= totJaunes ; ++nbJaunes) for (int dernier(0); dernier < 4; ++dernier) { int prix = dp[nbRouge][nbVerts][nbJaunes][dernier]; if (prix == INF) continue; int curPos[3] = {nbRouge, nbVerts, nbJaunes}; for (int prochain(0); prochain < 3; ++prochain) { if (curPos[prochain] >= (int)positions[prochain].size()-1) continue; if (prochain == dernier) continue; int nbSwap(0); for (int autres(0); autres < 3; ++autres) if (autres != prochain) { int posNxt = lower_bound(positions[autres].begin(),positions[autres].end(), positions[prochain][curPos[prochain]]) - positions[autres].begin(); if (posNxt > curPos[autres]) nbSwap += posNxt - curPos[autres]; } int nxtRouges = nbRouge, nxtVerts = nbVerts, nxtJaunes = nbJaunes; if (!prochain) nxtRouges++; else if (prochain == 1) nxtVerts++; else nxtJaunes++; dp[nxtRouges][nxtVerts][nxtJaunes][prochain] = min(dp[nxtRouges][nxtVerts][nxtJaunes][prochain], nbSwap + prix); } } for (int dernier(0); dernier < 3; ++dernier) rep = min(rep, dp[totRouges][totVerts][totJaunes][dernier]); if (rep == INF) rep = -1; cout << rep << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...