#include <bits/stdc++.h>
using namespace std;
const int maxn = 405;
const int inf = 1e9;
int n;
string s;
vector<int> pos[3];
int preffix[maxn][3];
int dp[maxn][maxn][maxn][3];
int decode(char c)
{
if (c == 'R')
{
return 0;
}
if (c == 'G')
{
return 1;
}
return 2;
}
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
cin >> n >> s;
s = '#' + s;
for (int i = 1; i <= n; ++i)
{
int curr = decode(s[i]);
for (int j = 0; j < 3; ++j)
{
preffix[i][j] = preffix[i - 1][j];
}
++preffix[i][curr];
pos[curr].push_back(i);
}
for (int i = 0; i <= n; ++i)
{
for (int j = 0; j <= n; ++j)
{
for (int k = 0; k <= n; ++k)
{
for (int p = 0; p <= 2; ++p)
{
dp[i][j][k][p] = inf;
}
}
}
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
int cnt0 = preffix[n][0];
int cnt1 = preffix[n][1];
int cnt2 = preffix[n][2];
for (int i = 0; i <= cnt0; ++i)
{
for (int j = 0; j <= cnt1; ++j)
{
for (int k = 0; k <= cnt2; ++k)
{
if (i != 0)
{
int curr_min = min(dp[i - 1][j][k][1], dp[i - 1][j][k][2]);
int cost = max(0, preffix[pos[0][i - 1]][1] - j) + max(0, preffix[pos[0][i - 1]][2] - k);
dp[i][j][k][0] = min(dp[i][j][k][0], curr_min + cost);
}
if (j != 0)
{
int curr_min = min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]);
int cost = max(0, preffix[pos[1][j - 1]][0] - i) + max(0, preffix[pos[1][j - 1]][2] - k);
dp[i][j][k][1] = min(dp[i][j][k][1], curr_min + cost);
}
if (k != 0)
{
int curr_min = min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]);
int cost = max(0, preffix[pos[2][k - 1]][0] - i) + max(0, preffix[pos[2][k - 1]][1] - j);
dp[i][j][k][2] = min(dp[i][j][k][2], curr_min + cost);
}
}
}
}
int ans = min(dp[cnt0][cnt1][cnt2][0], min(dp[cnt0][cnt1][cnt2][1], dp[cnt0][cnt1][cnt2][2]));
if (ans == inf)
{
cout << -1 << endl;
}
else
{
cout << ans << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1364 KB |
Output is correct |
7 |
Correct |
2 ms |
1356 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1364 KB |
Output is correct |
10 |
Correct |
1 ms |
1348 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1352 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
1 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1364 KB |
Output is correct |
7 |
Correct |
2 ms |
1356 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1364 KB |
Output is correct |
10 |
Correct |
1 ms |
1348 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1352 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
1 ms |
1236 KB |
Output is correct |
18 |
Correct |
7 ms |
18004 KB |
Output is correct |
19 |
Correct |
7 ms |
17992 KB |
Output is correct |
20 |
Correct |
7 ms |
18004 KB |
Output is correct |
21 |
Correct |
7 ms |
18040 KB |
Output is correct |
22 |
Correct |
8 ms |
18004 KB |
Output is correct |
23 |
Correct |
7 ms |
18004 KB |
Output is correct |
24 |
Correct |
8 ms |
17984 KB |
Output is correct |
25 |
Correct |
8 ms |
18004 KB |
Output is correct |
26 |
Correct |
8 ms |
18052 KB |
Output is correct |
27 |
Correct |
7 ms |
18004 KB |
Output is correct |
28 |
Correct |
9 ms |
18004 KB |
Output is correct |
29 |
Correct |
8 ms |
18004 KB |
Output is correct |
30 |
Correct |
8 ms |
18000 KB |
Output is correct |
31 |
Correct |
8 ms |
18004 KB |
Output is correct |
32 |
Correct |
8 ms |
18004 KB |
Output is correct |
33 |
Correct |
7 ms |
17364 KB |
Output is correct |
34 |
Correct |
7 ms |
17348 KB |
Output is correct |
35 |
Correct |
7 ms |
16852 KB |
Output is correct |
36 |
Correct |
7 ms |
17364 KB |
Output is correct |
37 |
Correct |
7 ms |
16204 KB |
Output is correct |
38 |
Correct |
7 ms |
18004 KB |
Output is correct |
39 |
Correct |
8 ms |
18004 KB |
Output is correct |
40 |
Correct |
7 ms |
17364 KB |
Output is correct |
41 |
Correct |
7 ms |
17988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Execution timed out |
528 ms |
766580 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1364 KB |
Output is correct |
7 |
Correct |
2 ms |
1356 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1364 KB |
Output is correct |
10 |
Correct |
1 ms |
1348 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1352 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
1 ms |
1236 KB |
Output is correct |
18 |
Correct |
7 ms |
18004 KB |
Output is correct |
19 |
Correct |
7 ms |
17992 KB |
Output is correct |
20 |
Correct |
7 ms |
18004 KB |
Output is correct |
21 |
Correct |
7 ms |
18040 KB |
Output is correct |
22 |
Correct |
8 ms |
18004 KB |
Output is correct |
23 |
Correct |
7 ms |
18004 KB |
Output is correct |
24 |
Correct |
8 ms |
17984 KB |
Output is correct |
25 |
Correct |
8 ms |
18004 KB |
Output is correct |
26 |
Correct |
8 ms |
18052 KB |
Output is correct |
27 |
Correct |
7 ms |
18004 KB |
Output is correct |
28 |
Correct |
9 ms |
18004 KB |
Output is correct |
29 |
Correct |
8 ms |
18004 KB |
Output is correct |
30 |
Correct |
8 ms |
18000 KB |
Output is correct |
31 |
Correct |
8 ms |
18004 KB |
Output is correct |
32 |
Correct |
8 ms |
18004 KB |
Output is correct |
33 |
Correct |
7 ms |
17364 KB |
Output is correct |
34 |
Correct |
7 ms |
17348 KB |
Output is correct |
35 |
Correct |
7 ms |
16852 KB |
Output is correct |
36 |
Correct |
7 ms |
17364 KB |
Output is correct |
37 |
Correct |
7 ms |
16204 KB |
Output is correct |
38 |
Correct |
7 ms |
18004 KB |
Output is correct |
39 |
Correct |
8 ms |
18004 KB |
Output is correct |
40 |
Correct |
7 ms |
17364 KB |
Output is correct |
41 |
Correct |
7 ms |
17988 KB |
Output is correct |
42 |
Correct |
1 ms |
328 KB |
Output is correct |
43 |
Execution timed out |
528 ms |
766580 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |