/*input
20
YYGYYYGGGGRGYYGRGRYG
*/
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int code(char c)
{
if (c == 'R')
return 0;
if (c == 'G')
return 1;
return 2;
}
int main()
{
ios_base::sync_with_stdio(false);
int n;
string s;
cin >> n >> s;
vector<int>p[3];
for (int i = 0; i < n; i++)
p[code(s[i])].push_back(i);
const int p0s = p[0].size();
const int p1s = p[1].size();
const int p2s = p[2].size();
int dp[p[0].size() + 2][p[1].size() + 2][p[2].size() + 2][3];
for (int k0 = 0; k0 <= p0s; k0++)
{
for (int k1 = 0; k1 <= p1s; k1++)
{
for (int k2 = 0; k2 <= p2s; k2++)
{
for (int a = 0; a < 3; a++)
dp[k0][k1][k2][a] = 2e8;
}
}
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
int d[3][n][n];
for (int a = 0; a < 3; a++)
{
for (int i = 0; i < n; i++)
{
auto it = lower_bound(p[a].begin(), p[a].end(), i);
for (int k = 0; k <= (int)p[a].size(); k++)
{
d[a][i][k] = max(it, p[a].begin() + k) - (p[a].begin() + k);
}
}
}
int k[3];
for (k[0] = 0; k[0] <= p0s; k[0]++)
{
for (k[1] = 0; k[1] <= p1s; k[1]++)
{
for (k[2] = 0; k[2] <= p2s; k[2]++)
{
for (int a = 0; a < 3; a++)
{
if (k[a] == (int)p[a].size())
continue;
int cost = d[0][p[a][k[a]]][k[0]] + d[1][p[a][k[a]]][k[1]] + d[2][p[a][k[a]]][k[2]];
if (a == 0)
{
dp[k[0] + 1][k[1]][k[2]][1] =
min(dp[k[0] + 1][k[1]][k[2]][1],
cost + dp[k[0]][k[1]][k[2]][0]);
dp[k[0] + 1][k[1]][k[2]][2] =
min(dp[k[0] + 1][k[1]][k[2]][2],
cost + dp[k[0]][k[1]][k[2]][0]);
}
else if (a == 1)
{
dp[k[0]][k[1] + 1][k[2]][0] =
min(dp[k[0]][k[1] + 1][k[2]][0],
cost + dp[k[0]][k[1]][k[2]][1]);
dp[k[0]][k[1] + 1][k[2]][2] =
min(dp[k[0]][k[1] + 1][k[2]][2],
cost + dp[k[0]][k[1]][k[2]][1]);
}
else
{
dp[k[0]][k[1]][k[2] + 1][0] =
min(dp[k[0]][k[1]][k[2] + 1][0],
cost + dp[k[0]][k[1]][k[2]][2]);
dp[k[0]][k[1]][k[2] + 1][1] =
min(dp[k[0]][k[1]][k[2] + 1][1],
cost + dp[k[0]][k[1]][k[2]][2]);
}
}
}
}
}
int ans = min({dp[p0s][p1s][p2s][0],
dp[p0s][p1s][p2s][1],
dp[p0s][p1s][p2s][2]
});
if (ans > 1e8)
ans = -1;
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
21 |
Correct |
2 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
512 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
512 KB |
Output is correct |
28 |
Correct |
2 ms |
512 KB |
Output is correct |
29 |
Correct |
2 ms |
512 KB |
Output is correct |
30 |
Correct |
2 ms |
512 KB |
Output is correct |
31 |
Correct |
2 ms |
512 KB |
Output is correct |
32 |
Correct |
2 ms |
512 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
34 |
Correct |
2 ms |
512 KB |
Output is correct |
35 |
Correct |
2 ms |
512 KB |
Output is correct |
36 |
Correct |
2 ms |
512 KB |
Output is correct |
37 |
Correct |
2 ms |
512 KB |
Output is correct |
38 |
Correct |
2 ms |
512 KB |
Output is correct |
39 |
Correct |
2 ms |
512 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
3200 KB |
Output is correct |
3 |
Correct |
4 ms |
3200 KB |
Output is correct |
4 |
Correct |
5 ms |
3200 KB |
Output is correct |
5 |
Correct |
4 ms |
3200 KB |
Output is correct |
6 |
Correct |
5 ms |
3200 KB |
Output is correct |
7 |
Correct |
5 ms |
3200 KB |
Output is correct |
8 |
Correct |
4 ms |
3200 KB |
Output is correct |
9 |
Correct |
5 ms |
3200 KB |
Output is correct |
10 |
Correct |
5 ms |
3200 KB |
Output is correct |
11 |
Correct |
4 ms |
3200 KB |
Output is correct |
12 |
Correct |
2 ms |
1152 KB |
Output is correct |
13 |
Correct |
3 ms |
1664 KB |
Output is correct |
14 |
Correct |
3 ms |
2304 KB |
Output is correct |
15 |
Correct |
4 ms |
3200 KB |
Output is correct |
16 |
Correct |
4 ms |
3200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
21 |
Correct |
2 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
512 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
512 KB |
Output is correct |
28 |
Correct |
2 ms |
512 KB |
Output is correct |
29 |
Correct |
2 ms |
512 KB |
Output is correct |
30 |
Correct |
2 ms |
512 KB |
Output is correct |
31 |
Correct |
2 ms |
512 KB |
Output is correct |
32 |
Correct |
2 ms |
512 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
34 |
Correct |
2 ms |
512 KB |
Output is correct |
35 |
Correct |
2 ms |
512 KB |
Output is correct |
36 |
Correct |
2 ms |
512 KB |
Output is correct |
37 |
Correct |
2 ms |
512 KB |
Output is correct |
38 |
Correct |
2 ms |
512 KB |
Output is correct |
39 |
Correct |
2 ms |
512 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
5 ms |
3200 KB |
Output is correct |
44 |
Correct |
4 ms |
3200 KB |
Output is correct |
45 |
Correct |
5 ms |
3200 KB |
Output is correct |
46 |
Correct |
4 ms |
3200 KB |
Output is correct |
47 |
Correct |
5 ms |
3200 KB |
Output is correct |
48 |
Correct |
5 ms |
3200 KB |
Output is correct |
49 |
Correct |
4 ms |
3200 KB |
Output is correct |
50 |
Correct |
5 ms |
3200 KB |
Output is correct |
51 |
Correct |
5 ms |
3200 KB |
Output is correct |
52 |
Correct |
4 ms |
3200 KB |
Output is correct |
53 |
Correct |
2 ms |
1152 KB |
Output is correct |
54 |
Correct |
3 ms |
1664 KB |
Output is correct |
55 |
Correct |
3 ms |
2304 KB |
Output is correct |
56 |
Correct |
4 ms |
3200 KB |
Output is correct |
57 |
Correct |
4 ms |
3200 KB |
Output is correct |
58 |
Correct |
59 ms |
30884 KB |
Output is correct |
59 |
Correct |
53 ms |
30900 KB |
Output is correct |
60 |
Correct |
52 ms |
30584 KB |
Output is correct |
61 |
Correct |
59 ms |
30956 KB |
Output is correct |
62 |
Correct |
8 ms |
4992 KB |
Output is correct |
63 |
Correct |
10 ms |
6784 KB |
Output is correct |
64 |
Correct |
27 ms |
16640 KB |
Output is correct |
65 |
Correct |
38 ms |
22656 KB |
Output is correct |
66 |
Correct |
51 ms |
30456 KB |
Output is correct |
67 |
Correct |
57 ms |
30464 KB |
Output is correct |
68 |
Correct |
50 ms |
30944 KB |
Output is correct |
69 |
Correct |
63 ms |
31096 KB |
Output is correct |
70 |
Correct |
60 ms |
30976 KB |
Output is correct |
71 |
Correct |
52 ms |
30336 KB |
Output is correct |
72 |
Correct |
15 ms |
9728 KB |
Output is correct |
73 |
Correct |
6 ms |
4096 KB |
Output is correct |