#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 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 |
2 ms |
1516 KB |
Output is correct |
6 |
Correct |
1 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 |
2 ms |
1516 KB |
Output is correct |
13 |
Correct |
2 ms |
1516 KB |
Output is correct |
14 |
Correct |
1 ms |
1516 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 |
492 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 |
2 ms |
1516 KB |
Output is correct |
6 |
Correct |
1 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 |
2 ms |
1516 KB |
Output is correct |
13 |
Correct |
2 ms |
1516 KB |
Output is correct |
14 |
Correct |
1 ms |
1516 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 |
13 ms |
22528 KB |
Output is correct |
19 |
Correct |
14 ms |
22508 KB |
Output is correct |
20 |
Correct |
13 ms |
22508 KB |
Output is correct |
21 |
Correct |
13 ms |
22508 KB |
Output is correct |
22 |
Correct |
13 ms |
22508 KB |
Output is correct |
23 |
Correct |
14 ms |
22508 KB |
Output is correct |
24 |
Correct |
13 ms |
22508 KB |
Output is correct |
25 |
Correct |
13 ms |
22508 KB |
Output is correct |
26 |
Correct |
13 ms |
22528 KB |
Output is correct |
27 |
Correct |
14 ms |
22636 KB |
Output is correct |
28 |
Correct |
14 ms |
22636 KB |
Output is correct |
29 |
Correct |
13 ms |
22508 KB |
Output is correct |
30 |
Correct |
13 ms |
22508 KB |
Output is correct |
31 |
Correct |
13 ms |
22508 KB |
Output is correct |
32 |
Correct |
13 ms |
22508 KB |
Output is correct |
33 |
Correct |
12 ms |
21740 KB |
Output is correct |
34 |
Correct |
12 ms |
21740 KB |
Output is correct |
35 |
Correct |
12 ms |
20844 KB |
Output is correct |
36 |
Correct |
15 ms |
21740 KB |
Output is correct |
37 |
Correct |
12 ms |
20076 KB |
Output is correct |
38 |
Correct |
13 ms |
22508 KB |
Output is correct |
39 |
Correct |
13 ms |
22508 KB |
Output is correct |
40 |
Correct |
12 ms |
21740 KB |
Output is correct |
41 |
Correct |
12 ms |
22528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Execution timed out |
557 ms |
1048580 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 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 |
2 ms |
1516 KB |
Output is correct |
6 |
Correct |
1 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 |
2 ms |
1516 KB |
Output is correct |
13 |
Correct |
2 ms |
1516 KB |
Output is correct |
14 |
Correct |
1 ms |
1516 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 |
13 ms |
22528 KB |
Output is correct |
19 |
Correct |
14 ms |
22508 KB |
Output is correct |
20 |
Correct |
13 ms |
22508 KB |
Output is correct |
21 |
Correct |
13 ms |
22508 KB |
Output is correct |
22 |
Correct |
13 ms |
22508 KB |
Output is correct |
23 |
Correct |
14 ms |
22508 KB |
Output is correct |
24 |
Correct |
13 ms |
22508 KB |
Output is correct |
25 |
Correct |
13 ms |
22508 KB |
Output is correct |
26 |
Correct |
13 ms |
22528 KB |
Output is correct |
27 |
Correct |
14 ms |
22636 KB |
Output is correct |
28 |
Correct |
14 ms |
22636 KB |
Output is correct |
29 |
Correct |
13 ms |
22508 KB |
Output is correct |
30 |
Correct |
13 ms |
22508 KB |
Output is correct |
31 |
Correct |
13 ms |
22508 KB |
Output is correct |
32 |
Correct |
13 ms |
22508 KB |
Output is correct |
33 |
Correct |
12 ms |
21740 KB |
Output is correct |
34 |
Correct |
12 ms |
21740 KB |
Output is correct |
35 |
Correct |
12 ms |
20844 KB |
Output is correct |
36 |
Correct |
15 ms |
21740 KB |
Output is correct |
37 |
Correct |
12 ms |
20076 KB |
Output is correct |
38 |
Correct |
13 ms |
22508 KB |
Output is correct |
39 |
Correct |
13 ms |
22508 KB |
Output is correct |
40 |
Correct |
12 ms |
21740 KB |
Output is correct |
41 |
Correct |
12 ms |
22528 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Execution timed out |
557 ms |
1048580 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |