#include <vector>
#include <iostream>
#include <algorithm>
#define int long long
using namespace std;
struct prefsum
{
int n;
vector<int> vals;
void init(vector<int> ip)
{
n = ip.size();
vals.resize(n + 1);
for (int i = 1; i <= n; i++)
{
vals[i] = vals[i - 1] + ip[i - 1];
}
}
int operator()(int l, int r)
{
return vals[r] - vals[l];
}
};
struct dpstruct
{
prefsum veg_R;
prefsum veg_Y;
prefsum veg_G;
vector<int> pos_R;
vector<int> pos_Y;
vector<int> pos_G;
int R = 0, Y = 0, G = 0;
vector<vector<vector<vector<int>>>> table;
int dp(int r, int y, int g, int last)
{
if (r < 0 || y < 0 || g < 0) return 1e15;
if (r + y + g == 0) return 0;
if (table[r][y][g][last] != -1) return table[r][y][g][last];
int cost = 1e15;
if (last == 0 && r != 0)
{
cost = min(dp(r - 1, y, g, 1), dp(r - 1, y, g, 2)) + max(0LL, veg_Y(0, pos_R[r - 1]) - y) + max(0LL, veg_G(0, pos_R[r - 1]) - g);
}
else if (last == 1 && y != 0)
{
cost = min(dp(r, y - 1, g, 0), dp(r, y - 1, g, 2)) + max(0LL, veg_R(0, pos_Y[y - 1]) - r) + max(0LL, veg_G(0, pos_Y[y - 1]) - g);
}
else if (last == 2 && g != 0)
{
cost = min(dp(r, y, g - 1, 0), dp(r, y, g - 1, 1)) + max(0LL, veg_Y(0, pos_G[g - 1]) - y) + max(0LL, veg_R(0, pos_G[g - 1]) - r);
}
return table[r][y][g][last] = cost;
}
void init(int n, string s)
{
vector<int> rs(n), ys(n), gs(n);
for (int i = 0; i < n; i++)
{
char c = s[i];
if (c == 'R')
{
R++;
rs[i]++;
pos_R.push_back(i);
}
else if (c == 'Y')
{
Y++;
ys[i]++;
pos_Y.push_back(i);
}
else
{
G++;
gs[i]++;
pos_G.push_back(i);
}
}
veg_R.init(rs);
veg_Y.init(ys);
veg_G.init(gs);
table.resize(R + 1, vector<vector<vector<int>>>(Y + 1, vector<vector<int>>(G + 1, vector<int>(3, -1))));
}
};
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
dpstruct dps;
dps.init(n, s);
int mmin = 1e18;
for (int i = 0; i < 3; i++) mmin = min(mmin, dps.dp(dps.R, dps.Y, dps.G, i));
if (mmin >= 1e15) cout << "-1\n";
else cout << mmin << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
888 KB |
Output is correct |
19 |
Correct |
3 ms |
888 KB |
Output is correct |
20 |
Correct |
4 ms |
892 KB |
Output is correct |
21 |
Correct |
3 ms |
888 KB |
Output is correct |
22 |
Correct |
3 ms |
888 KB |
Output is correct |
23 |
Correct |
4 ms |
888 KB |
Output is correct |
24 |
Correct |
3 ms |
760 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
504 KB |
Output is correct |
27 |
Correct |
3 ms |
760 KB |
Output is correct |
28 |
Correct |
3 ms |
888 KB |
Output is correct |
29 |
Correct |
3 ms |
888 KB |
Output is correct |
30 |
Correct |
3 ms |
888 KB |
Output is correct |
31 |
Correct |
3 ms |
888 KB |
Output is correct |
32 |
Correct |
4 ms |
888 KB |
Output is correct |
33 |
Correct |
3 ms |
504 KB |
Output is correct |
34 |
Correct |
3 ms |
632 KB |
Output is correct |
35 |
Correct |
3 ms |
764 KB |
Output is correct |
36 |
Correct |
5 ms |
888 KB |
Output is correct |
37 |
Correct |
3 ms |
828 KB |
Output is correct |
38 |
Correct |
4 ms |
888 KB |
Output is correct |
39 |
Correct |
12 ms |
860 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
2680 KB |
Output is correct |
3 |
Correct |
6 ms |
2556 KB |
Output is correct |
4 |
Correct |
6 ms |
2680 KB |
Output is correct |
5 |
Correct |
6 ms |
2664 KB |
Output is correct |
6 |
Correct |
6 ms |
2680 KB |
Output is correct |
7 |
Correct |
6 ms |
2680 KB |
Output is correct |
8 |
Correct |
6 ms |
2552 KB |
Output is correct |
9 |
Correct |
6 ms |
2680 KB |
Output is correct |
10 |
Correct |
6 ms |
2680 KB |
Output is correct |
11 |
Correct |
6 ms |
2680 KB |
Output is correct |
12 |
Correct |
3 ms |
1016 KB |
Output is correct |
13 |
Correct |
4 ms |
1528 KB |
Output is correct |
14 |
Correct |
5 ms |
1912 KB |
Output is correct |
15 |
Correct |
7 ms |
2680 KB |
Output is correct |
16 |
Correct |
6 ms |
2680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
888 KB |
Output is correct |
19 |
Correct |
3 ms |
888 KB |
Output is correct |
20 |
Correct |
4 ms |
892 KB |
Output is correct |
21 |
Correct |
3 ms |
888 KB |
Output is correct |
22 |
Correct |
3 ms |
888 KB |
Output is correct |
23 |
Correct |
4 ms |
888 KB |
Output is correct |
24 |
Correct |
3 ms |
760 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
504 KB |
Output is correct |
27 |
Correct |
3 ms |
760 KB |
Output is correct |
28 |
Correct |
3 ms |
888 KB |
Output is correct |
29 |
Correct |
3 ms |
888 KB |
Output is correct |
30 |
Correct |
3 ms |
888 KB |
Output is correct |
31 |
Correct |
3 ms |
888 KB |
Output is correct |
32 |
Correct |
4 ms |
888 KB |
Output is correct |
33 |
Correct |
3 ms |
504 KB |
Output is correct |
34 |
Correct |
3 ms |
632 KB |
Output is correct |
35 |
Correct |
3 ms |
764 KB |
Output is correct |
36 |
Correct |
5 ms |
888 KB |
Output is correct |
37 |
Correct |
3 ms |
828 KB |
Output is correct |
38 |
Correct |
4 ms |
888 KB |
Output is correct |
39 |
Correct |
12 ms |
860 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
504 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |
43 |
Correct |
6 ms |
2680 KB |
Output is correct |
44 |
Correct |
6 ms |
2556 KB |
Output is correct |
45 |
Correct |
6 ms |
2680 KB |
Output is correct |
46 |
Correct |
6 ms |
2664 KB |
Output is correct |
47 |
Correct |
6 ms |
2680 KB |
Output is correct |
48 |
Correct |
6 ms |
2680 KB |
Output is correct |
49 |
Correct |
6 ms |
2552 KB |
Output is correct |
50 |
Correct |
6 ms |
2680 KB |
Output is correct |
51 |
Correct |
6 ms |
2680 KB |
Output is correct |
52 |
Correct |
6 ms |
2680 KB |
Output is correct |
53 |
Correct |
3 ms |
1016 KB |
Output is correct |
54 |
Correct |
4 ms |
1528 KB |
Output is correct |
55 |
Correct |
5 ms |
1912 KB |
Output is correct |
56 |
Correct |
7 ms |
2680 KB |
Output is correct |
57 |
Correct |
6 ms |
2680 KB |
Output is correct |
58 |
Execution timed out |
580 ms |
133604 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |