#include <bits/stdc++.h>
#define int long long
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sz size()
#define yes "YES"
#define no "NO"
#define IOI ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pf push_front
#define pb push_back
#define S second
#define F first
using namespace std;
const int N = 400 + 1;
const int NN = 1e5;
const int mod = (1e9 + 7);
const int inf = 1e18;
int n, pref[3][N];
vector<int> g[3];
string s;
void legenda_ne_umret() {
cin >> n >> s;
s = '+' + s;
for (int i = 1; i <= n; i++) {
if (s[i] == 'Y') {
g[0].pb(i);
}
if (s[i] == 'R') {
g[1].pb(i);
}
if (s[i] == 'G') {
g[2].pb(i);
}
pref[0][i] = pref[0][i - 1] + (s[i] == 'Y');
pref[1][i] = pref[1][i - 1] + (s[i] == 'R');
pref[2][i] = pref[2][i - 1] + (s[i] == 'G');
}
int allr = pref[1][n];
int ally = pref[0][n];
int allg = pref[2][n];
int dp[n + 1][allr + 1][ally + 1][3];
for (int id = 1; id <= n; id++) {
for (int a = 0; a <= allr; a++) {
for (int b = 0; b <= ally; b++) {
dp[id][a][b][0] = inf;
dp[id][a][b][1] = inf;
dp[id][a][b][2] = inf;
}
}
}
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for (int id = 1; id <= n; id++) {
for (int a = 0; a <= allr ; a++) {
for (int b = 0; b <= ally; b++) {
int c = id - a - b;
if(c>allg || a + b > id)continue;
if (a > 0) {
int pos = g[1][a - 1];
int cnt = max(0ll , b - pref[0][pos]) + max(0ll , c - pref[2][pos]);
dp[id][a][b][0] = min(dp[id - 1][a - 1][b][1], dp[id - 1][a - 1][b][2]) + max(0ll, (pos + cnt) - id);
}
if (b > 0) {
int pos = g[0][b - 1];
int cnt = max(0ll , a - pref[1][pos]) + max(0ll , c - pref[2][pos]);
dp[id][a][b][1] = min(dp[id - 1][a][b - 1][0], dp[id - 1][a][b - 1][2]) + max(0ll, (pos + cnt) - id);
}
if (c > 0) {
int pos = g[2][c - 1];
int cnt = max(0ll , b - pref[0][pos]) + max(0ll , a - pref[1][pos]);
dp[id][a][b][2] = min(dp[id - 1][a][b][0], dp[id - 1][a][b][1]) + max(0ll, (pos + cnt) - id);
}
}
}
}
cout << (min({dp[n][allr][ally][0], dp[n][allr][ally][1], dp[n][allr][ally][2]}) >= inf ? -1 : min({dp[n][allr][ally][0], dp[n][allr][ally][1], dp[n][allr][ally][2]}));
}
signed main() {
IOI;
// freopen("maze.in", "r", stdin);
// freopen("maze.out", "w", stdout);
/////////////////////////////////////////////
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
// cout << "Case " << i << ":\n";
legenda_ne_umret();
cout << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
1 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
856 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
1116 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
0 ms |
860 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
0 ms |
716 KB |
Output is correct |
36 |
Correct |
1 ms |
1116 KB |
Output is correct |
37 |
Correct |
1 ms |
860 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
2140 KB |
Output is correct |
3 |
Correct |
2 ms |
2140 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2140 KB |
Output is correct |
6 |
Correct |
1 ms |
2140 KB |
Output is correct |
7 |
Correct |
1 ms |
2140 KB |
Output is correct |
8 |
Correct |
1 ms |
2140 KB |
Output is correct |
9 |
Correct |
2 ms |
2140 KB |
Output is correct |
10 |
Correct |
2 ms |
2140 KB |
Output is correct |
11 |
Correct |
1 ms |
2140 KB |
Output is correct |
12 |
Correct |
0 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
1352 KB |
Output is correct |
14 |
Correct |
1 ms |
1880 KB |
Output is correct |
15 |
Correct |
2 ms |
2136 KB |
Output is correct |
16 |
Correct |
1 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
1 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
856 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
1116 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
0 ms |
860 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
0 ms |
716 KB |
Output is correct |
36 |
Correct |
1 ms |
1116 KB |
Output is correct |
37 |
Correct |
1 ms |
860 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
344 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
2 ms |
2140 KB |
Output is correct |
44 |
Correct |
2 ms |
2140 KB |
Output is correct |
45 |
Correct |
2 ms |
2396 KB |
Output is correct |
46 |
Correct |
2 ms |
2140 KB |
Output is correct |
47 |
Correct |
1 ms |
2140 KB |
Output is correct |
48 |
Correct |
1 ms |
2140 KB |
Output is correct |
49 |
Correct |
1 ms |
2140 KB |
Output is correct |
50 |
Correct |
2 ms |
2140 KB |
Output is correct |
51 |
Correct |
2 ms |
2140 KB |
Output is correct |
52 |
Correct |
1 ms |
2140 KB |
Output is correct |
53 |
Correct |
0 ms |
860 KB |
Output is correct |
54 |
Correct |
1 ms |
1352 KB |
Output is correct |
55 |
Correct |
1 ms |
1880 KB |
Output is correct |
56 |
Correct |
2 ms |
2136 KB |
Output is correct |
57 |
Correct |
1 ms |
2140 KB |
Output is correct |
58 |
Correct |
127 ms |
176724 KB |
Output is correct |
59 |
Correct |
113 ms |
162896 KB |
Output is correct |
60 |
Correct |
114 ms |
174416 KB |
Output is correct |
61 |
Correct |
112 ms |
171636 KB |
Output is correct |
62 |
Correct |
7 ms |
10328 KB |
Output is correct |
63 |
Correct |
12 ms |
16988 KB |
Output is correct |
64 |
Correct |
42 ms |
62220 KB |
Output is correct |
65 |
Correct |
59 ms |
87380 KB |
Output is correct |
66 |
Correct |
113 ms |
169360 KB |
Output is correct |
67 |
Correct |
103 ms |
146004 KB |
Output is correct |
68 |
Correct |
118 ms |
167504 KB |
Output is correct |
69 |
Correct |
120 ms |
175700 KB |
Output is correct |
70 |
Correct |
113 ms |
160368 KB |
Output is correct |
71 |
Correct |
125 ms |
193188 KB |
Output is correct |
72 |
Correct |
123 ms |
204628 KB |
Output is correct |
73 |
Correct |
9 ms |
14168 KB |
Output is correct |