#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;
dp[3][0][0][0] = 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;
//cout << dernier << ' ' << nbRouge << ' ' << nbVerts << ' ' << nbJaunes << ' ' << prix << endl;
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;
//cout << "HA" << endl;
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];
}
//cout << "HA2 " << nbSwap << ' ' << prochain << ' ' << prix << endl;
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 time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |