#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][3];
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 < 3; ++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);
if (totRouges)
dp[1][0][0][0] = positions[0][0];
if (totVerts)
dp[0][1][0][1] = positions[1][0];
if (totJaunes)
dp[0][0][1][2] = positions[2][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 < 3; ++dernier)
if (dp[nbRouge][nbVerts][nbJaunes][dernier] < INF)
{
int prix = dp[nbRouge][nbVerts][nbJaunes][dernier];
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
512 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 |
1516 KB |
Output is correct |
6 |
Correct |
2 ms |
1516 KB |
Output is correct |
7 |
Correct |
1 ms |
1516 KB |
Output is correct |
8 |
Correct |
1 ms |
1516 KB |
Output is correct |
9 |
Correct |
1 ms |
1516 KB |
Output is correct |
10 |
Correct |
1 ms |
1516 KB |
Output is correct |
11 |
Correct |
1 ms |
1516 KB |
Output is correct |
12 |
Correct |
1 ms |
1516 KB |
Output is correct |
13 |
Correct |
1 ms |
1516 KB |
Output is correct |
14 |
Correct |
2 ms |
1388 KB |
Output is correct |
15 |
Correct |
1 ms |
1516 KB |
Output is correct |
16 |
Correct |
1 ms |
1516 KB |
Output is correct |
17 |
Correct |
1 ms |
1388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
512 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 |
1516 KB |
Output is correct |
6 |
Correct |
2 ms |
1516 KB |
Output is correct |
7 |
Correct |
1 ms |
1516 KB |
Output is correct |
8 |
Correct |
1 ms |
1516 KB |
Output is correct |
9 |
Correct |
1 ms |
1516 KB |
Output is correct |
10 |
Correct |
1 ms |
1516 KB |
Output is correct |
11 |
Correct |
1 ms |
1516 KB |
Output is correct |
12 |
Correct |
1 ms |
1516 KB |
Output is correct |
13 |
Correct |
1 ms |
1516 KB |
Output is correct |
14 |
Correct |
2 ms |
1388 KB |
Output is correct |
15 |
Correct |
1 ms |
1516 KB |
Output is correct |
16 |
Correct |
1 ms |
1516 KB |
Output is correct |
17 |
Correct |
1 ms |
1388 KB |
Output is correct |
18 |
Correct |
11 ms |
18028 KB |
Output is correct |
19 |
Correct |
11 ms |
18028 KB |
Output is correct |
20 |
Correct |
13 ms |
18028 KB |
Output is correct |
21 |
Correct |
13 ms |
18028 KB |
Output is correct |
22 |
Correct |
10 ms |
18028 KB |
Output is correct |
23 |
Correct |
10 ms |
18028 KB |
Output is correct |
24 |
Correct |
10 ms |
18028 KB |
Output is correct |
25 |
Correct |
10 ms |
18028 KB |
Output is correct |
26 |
Correct |
10 ms |
18028 KB |
Output is correct |
27 |
Correct |
10 ms |
18028 KB |
Output is correct |
28 |
Correct |
11 ms |
18048 KB |
Output is correct |
29 |
Correct |
11 ms |
18028 KB |
Output is correct |
30 |
Correct |
11 ms |
18028 KB |
Output is correct |
31 |
Correct |
11 ms |
18028 KB |
Output is correct |
32 |
Correct |
11 ms |
18028 KB |
Output is correct |
33 |
Correct |
10 ms |
17516 KB |
Output is correct |
34 |
Correct |
10 ms |
17516 KB |
Output is correct |
35 |
Correct |
10 ms |
16876 KB |
Output is correct |
36 |
Correct |
10 ms |
17516 KB |
Output is correct |
37 |
Correct |
10 ms |
16236 KB |
Output is correct |
38 |
Correct |
11 ms |
18028 KB |
Output is correct |
39 |
Correct |
11 ms |
18028 KB |
Output is correct |
40 |
Correct |
12 ms |
17516 KB |
Output is correct |
41 |
Correct |
10 ms |
18028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
411 ms |
757612 KB |
Output is correct |
3 |
Correct |
407 ms |
755328 KB |
Output is correct |
4 |
Correct |
409 ms |
757484 KB |
Output is correct |
5 |
Correct |
420 ms |
757508 KB |
Output is correct |
6 |
Correct |
419 ms |
757516 KB |
Output is correct |
7 |
Correct |
414 ms |
755308 KB |
Output is correct |
8 |
Correct |
408 ms |
755324 KB |
Output is correct |
9 |
Correct |
403 ms |
751596 KB |
Output is correct |
10 |
Correct |
429 ms |
757556 KB |
Output is correct |
11 |
Correct |
459 ms |
757484 KB |
Output is correct |
12 |
Correct |
112 ms |
202860 KB |
Output is correct |
13 |
Correct |
197 ms |
357484 KB |
Output is correct |
14 |
Correct |
288 ms |
517484 KB |
Output is correct |
15 |
Correct |
406 ms |
757484 KB |
Output is correct |
16 |
Correct |
411 ms |
757520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
512 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 |
1516 KB |
Output is correct |
6 |
Correct |
2 ms |
1516 KB |
Output is correct |
7 |
Correct |
1 ms |
1516 KB |
Output is correct |
8 |
Correct |
1 ms |
1516 KB |
Output is correct |
9 |
Correct |
1 ms |
1516 KB |
Output is correct |
10 |
Correct |
1 ms |
1516 KB |
Output is correct |
11 |
Correct |
1 ms |
1516 KB |
Output is correct |
12 |
Correct |
1 ms |
1516 KB |
Output is correct |
13 |
Correct |
1 ms |
1516 KB |
Output is correct |
14 |
Correct |
2 ms |
1388 KB |
Output is correct |
15 |
Correct |
1 ms |
1516 KB |
Output is correct |
16 |
Correct |
1 ms |
1516 KB |
Output is correct |
17 |
Correct |
1 ms |
1388 KB |
Output is correct |
18 |
Correct |
11 ms |
18028 KB |
Output is correct |
19 |
Correct |
11 ms |
18028 KB |
Output is correct |
20 |
Correct |
13 ms |
18028 KB |
Output is correct |
21 |
Correct |
13 ms |
18028 KB |
Output is correct |
22 |
Correct |
10 ms |
18028 KB |
Output is correct |
23 |
Correct |
10 ms |
18028 KB |
Output is correct |
24 |
Correct |
10 ms |
18028 KB |
Output is correct |
25 |
Correct |
10 ms |
18028 KB |
Output is correct |
26 |
Correct |
10 ms |
18028 KB |
Output is correct |
27 |
Correct |
10 ms |
18028 KB |
Output is correct |
28 |
Correct |
11 ms |
18048 KB |
Output is correct |
29 |
Correct |
11 ms |
18028 KB |
Output is correct |
30 |
Correct |
11 ms |
18028 KB |
Output is correct |
31 |
Correct |
11 ms |
18028 KB |
Output is correct |
32 |
Correct |
11 ms |
18028 KB |
Output is correct |
33 |
Correct |
10 ms |
17516 KB |
Output is correct |
34 |
Correct |
10 ms |
17516 KB |
Output is correct |
35 |
Correct |
10 ms |
16876 KB |
Output is correct |
36 |
Correct |
10 ms |
17516 KB |
Output is correct |
37 |
Correct |
10 ms |
16236 KB |
Output is correct |
38 |
Correct |
11 ms |
18028 KB |
Output is correct |
39 |
Correct |
11 ms |
18028 KB |
Output is correct |
40 |
Correct |
12 ms |
17516 KB |
Output is correct |
41 |
Correct |
10 ms |
18028 KB |
Output is correct |
42 |
Correct |
1 ms |
512 KB |
Output is correct |
43 |
Correct |
411 ms |
757612 KB |
Output is correct |
44 |
Correct |
407 ms |
755328 KB |
Output is correct |
45 |
Correct |
409 ms |
757484 KB |
Output is correct |
46 |
Correct |
420 ms |
757508 KB |
Output is correct |
47 |
Correct |
419 ms |
757516 KB |
Output is correct |
48 |
Correct |
414 ms |
755308 KB |
Output is correct |
49 |
Correct |
408 ms |
755324 KB |
Output is correct |
50 |
Correct |
403 ms |
751596 KB |
Output is correct |
51 |
Correct |
429 ms |
757556 KB |
Output is correct |
52 |
Correct |
459 ms |
757484 KB |
Output is correct |
53 |
Correct |
112 ms |
202860 KB |
Output is correct |
54 |
Correct |
197 ms |
357484 KB |
Output is correct |
55 |
Correct |
288 ms |
517484 KB |
Output is correct |
56 |
Correct |
406 ms |
757484 KB |
Output is correct |
57 |
Correct |
411 ms |
757520 KB |
Output is correct |
58 |
Execution timed out |
609 ms |
757484 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |