#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const int MAXN = 401;
const int INF = 1e9;
int dp[MAXN][MAXN][MAXN][4];
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);
vector<vector<int>> prefixSum(3, vector<int>(nbFleurs+1));
for (int i(0); i < nbFleurs; ++i)
{
for (int j(0); j < 3; ++j)
prefixSum[j][i+1] = prefixSum[j][i];
if (fleurs[i] == 'R')
{
positions[0].push_back(i);
prefixSum[0][i+1]++;
}
else if (fleurs[i] == 'G')
{
positions[1].push_back(i);
prefixSum[1][i+1]++;
}
else
{
positions[2].push_back(i);
prefixSum[2][i+1]++;
}
}
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();
int totVerts = (int)positions[1].size();
int totJaunes = (int)positions[2].size();
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())
continue;
if (prochain == dernier) continue;
int nbSwap(0);
for (int autres(0); autres < 3; ++autres)
if (autres != prochain and curPos[autres] < (int)positions[autres].size())
{
int posNxt = positions[prochain][curPos[prochain]];
int posAutres = positions[autres][curPos[autres]];
if (posNxt > posAutres)
nbSwap += prefixSum[autres][posNxt+1] - prefixSum[autres][posAutres];
}
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 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
876 KB |
Output is correct |
5 |
Correct |
1 ms |
1388 KB |
Output is correct |
6 |
Correct |
1 ms |
1388 KB |
Output is correct |
7 |
Correct |
1 ms |
1388 KB |
Output is correct |
8 |
Correct |
1 ms |
1388 KB |
Output is correct |
9 |
Correct |
1 ms |
1388 KB |
Output is correct |
10 |
Correct |
1 ms |
1388 KB |
Output is correct |
11 |
Correct |
1 ms |
1664 KB |
Output is correct |
12 |
Correct |
1 ms |
1388 KB |
Output is correct |
13 |
Correct |
1 ms |
1388 KB |
Output is correct |
14 |
Correct |
1 ms |
1260 KB |
Output is correct |
15 |
Correct |
2 ms |
1388 KB |
Output is correct |
16 |
Correct |
1 ms |
1388 KB |
Output is correct |
17 |
Correct |
1 ms |
1260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
876 KB |
Output is correct |
5 |
Correct |
1 ms |
1388 KB |
Output is correct |
6 |
Correct |
1 ms |
1388 KB |
Output is correct |
7 |
Correct |
1 ms |
1388 KB |
Output is correct |
8 |
Correct |
1 ms |
1388 KB |
Output is correct |
9 |
Correct |
1 ms |
1388 KB |
Output is correct |
10 |
Correct |
1 ms |
1388 KB |
Output is correct |
11 |
Correct |
1 ms |
1664 KB |
Output is correct |
12 |
Correct |
1 ms |
1388 KB |
Output is correct |
13 |
Correct |
1 ms |
1388 KB |
Output is correct |
14 |
Correct |
1 ms |
1260 KB |
Output is correct |
15 |
Correct |
2 ms |
1388 KB |
Output is correct |
16 |
Correct |
1 ms |
1388 KB |
Output is correct |
17 |
Correct |
1 ms |
1260 KB |
Output is correct |
18 |
Correct |
11 ms |
19052 KB |
Output is correct |
19 |
Correct |
12 ms |
19052 KB |
Output is correct |
20 |
Correct |
11 ms |
18924 KB |
Output is correct |
21 |
Correct |
11 ms |
19052 KB |
Output is correct |
22 |
Correct |
11 ms |
18924 KB |
Output is correct |
23 |
Correct |
11 ms |
19180 KB |
Output is correct |
24 |
Correct |
11 ms |
18924 KB |
Output is correct |
25 |
Correct |
10 ms |
19052 KB |
Output is correct |
26 |
Correct |
10 ms |
18924 KB |
Output is correct |
27 |
Correct |
11 ms |
18924 KB |
Output is correct |
28 |
Correct |
11 ms |
18924 KB |
Output is correct |
29 |
Correct |
11 ms |
18924 KB |
Output is correct |
30 |
Correct |
11 ms |
18924 KB |
Output is correct |
31 |
Correct |
11 ms |
18924 KB |
Output is correct |
32 |
Correct |
13 ms |
18924 KB |
Output is correct |
33 |
Correct |
10 ms |
18284 KB |
Output is correct |
34 |
Correct |
10 ms |
18284 KB |
Output is correct |
35 |
Correct |
11 ms |
17644 KB |
Output is correct |
36 |
Correct |
11 ms |
18284 KB |
Output is correct |
37 |
Correct |
10 ms |
17004 KB |
Output is correct |
38 |
Correct |
11 ms |
18924 KB |
Output is correct |
39 |
Correct |
13 ms |
19052 KB |
Output is correct |
40 |
Correct |
10 ms |
18284 KB |
Output is correct |
41 |
Correct |
11 ms |
18924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Execution timed out |
545 ms |
1009772 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
876 KB |
Output is correct |
5 |
Correct |
1 ms |
1388 KB |
Output is correct |
6 |
Correct |
1 ms |
1388 KB |
Output is correct |
7 |
Correct |
1 ms |
1388 KB |
Output is correct |
8 |
Correct |
1 ms |
1388 KB |
Output is correct |
9 |
Correct |
1 ms |
1388 KB |
Output is correct |
10 |
Correct |
1 ms |
1388 KB |
Output is correct |
11 |
Correct |
1 ms |
1664 KB |
Output is correct |
12 |
Correct |
1 ms |
1388 KB |
Output is correct |
13 |
Correct |
1 ms |
1388 KB |
Output is correct |
14 |
Correct |
1 ms |
1260 KB |
Output is correct |
15 |
Correct |
2 ms |
1388 KB |
Output is correct |
16 |
Correct |
1 ms |
1388 KB |
Output is correct |
17 |
Correct |
1 ms |
1260 KB |
Output is correct |
18 |
Correct |
11 ms |
19052 KB |
Output is correct |
19 |
Correct |
12 ms |
19052 KB |
Output is correct |
20 |
Correct |
11 ms |
18924 KB |
Output is correct |
21 |
Correct |
11 ms |
19052 KB |
Output is correct |
22 |
Correct |
11 ms |
18924 KB |
Output is correct |
23 |
Correct |
11 ms |
19180 KB |
Output is correct |
24 |
Correct |
11 ms |
18924 KB |
Output is correct |
25 |
Correct |
10 ms |
19052 KB |
Output is correct |
26 |
Correct |
10 ms |
18924 KB |
Output is correct |
27 |
Correct |
11 ms |
18924 KB |
Output is correct |
28 |
Correct |
11 ms |
18924 KB |
Output is correct |
29 |
Correct |
11 ms |
18924 KB |
Output is correct |
30 |
Correct |
11 ms |
18924 KB |
Output is correct |
31 |
Correct |
11 ms |
18924 KB |
Output is correct |
32 |
Correct |
13 ms |
18924 KB |
Output is correct |
33 |
Correct |
10 ms |
18284 KB |
Output is correct |
34 |
Correct |
10 ms |
18284 KB |
Output is correct |
35 |
Correct |
11 ms |
17644 KB |
Output is correct |
36 |
Correct |
11 ms |
18284 KB |
Output is correct |
37 |
Correct |
10 ms |
17004 KB |
Output is correct |
38 |
Correct |
11 ms |
18924 KB |
Output is correct |
39 |
Correct |
13 ms |
19052 KB |
Output is correct |
40 |
Correct |
10 ms |
18284 KB |
Output is correct |
41 |
Correct |
11 ms |
18924 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Execution timed out |
545 ms |
1009772 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |