#include <bits/stdc++.h>
#define task "HOT3"
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
using namespace std;
const int MAXN = 4e2 + 5;
const ll INF = 1e8 + 5;
int n, dp[MAXN][MAXN][MAXN][3];
string s;
void Input()
{
cin >> n >> s;
s = "," + s;
}
int cnt1[MAXN][3], cnt2[MAXN][3], cnt3[MAXN][3];
void Solve()
{
int cntr = 0, cntg = 0, cnty = 0;
for (int i = 1; i <= n; i++)
{
if (s[i] == 'R')
{
cntr++;
cnt1[cntr][1] = cntg;
cnt1[cntr][2] = cnty;
}
if (s[i] == 'G')
{
cntg++;
cnt2[cntg][0] = cntr;
cnt2[cntg][2] = cnty;
}
if (s[i] == 'Y')
{
cnty++;
cnt3[cnty][0] = cntr;
cnt3[cnty][1] = cntg;
}
}
memset(dp, 44, sizeof(dp));
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for (int i = 0; i <= cntr; i++)
{
for (int j = 0; j <= cntg; j++)
{
for (int z = 0; z <= cnty; z++)
{
if (i < cntr)
dp[i + 1][j][z][0] = min(dp[i + 1][j][z][0], min(dp[i][j][z][1], dp[i][j][z][2]) + abs(cnt1[i + 1][1] - j) + abs(cnt1[i + 1][2] - z));
if (j < cntg)
dp[i][j + 1][z][1] = min(dp[i][j + 1][z][1], min(dp[i][j][z][0], dp[i][j][z][2]) + abs(cnt2[j + 1][0] - i) + abs(cnt2[j + 1][2] - z));
if (z < cnty)
dp[i][j][z + 1][2] = min(dp[i][j][z + 1][2], min(dp[i][j][z][0], dp[i][j][z][1]) + abs(cnt3[z + 1][0] - i) + abs(cnt3[z + 1][1] - j));
}
}
}
int ans = min(dp[cntr][cntg][cnty][0], min(dp[cntr][cntg][cnty][1], dp[cntr][cntg][cnty][2]));
ans /= 2;
cout << (ans < INF ? ans : -1);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen(taskname".INP" , "r" , stdin);
//freopen(taskname".OUT" , "w" , stdout);
Input();
Solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
369 ms |
780228 KB |
Output is correct |
2 |
Correct |
363 ms |
780276 KB |
Output is correct |
3 |
Correct |
326 ms |
780232 KB |
Output is correct |
4 |
Correct |
344 ms |
780216 KB |
Output is correct |
5 |
Correct |
300 ms |
780264 KB |
Output is correct |
6 |
Correct |
336 ms |
780252 KB |
Output is correct |
7 |
Correct |
304 ms |
780304 KB |
Output is correct |
8 |
Correct |
308 ms |
780204 KB |
Output is correct |
9 |
Correct |
360 ms |
780280 KB |
Output is correct |
10 |
Correct |
288 ms |
780272 KB |
Output is correct |
11 |
Correct |
302 ms |
780252 KB |
Output is correct |
12 |
Correct |
296 ms |
780260 KB |
Output is correct |
13 |
Correct |
307 ms |
780272 KB |
Output is correct |
14 |
Correct |
302 ms |
780216 KB |
Output is correct |
15 |
Correct |
326 ms |
780292 KB |
Output is correct |
16 |
Correct |
339 ms |
780328 KB |
Output is correct |
17 |
Correct |
319 ms |
780312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
369 ms |
780228 KB |
Output is correct |
2 |
Correct |
363 ms |
780276 KB |
Output is correct |
3 |
Correct |
326 ms |
780232 KB |
Output is correct |
4 |
Correct |
344 ms |
780216 KB |
Output is correct |
5 |
Correct |
300 ms |
780264 KB |
Output is correct |
6 |
Correct |
336 ms |
780252 KB |
Output is correct |
7 |
Correct |
304 ms |
780304 KB |
Output is correct |
8 |
Correct |
308 ms |
780204 KB |
Output is correct |
9 |
Correct |
360 ms |
780280 KB |
Output is correct |
10 |
Correct |
288 ms |
780272 KB |
Output is correct |
11 |
Correct |
302 ms |
780252 KB |
Output is correct |
12 |
Correct |
296 ms |
780260 KB |
Output is correct |
13 |
Correct |
307 ms |
780272 KB |
Output is correct |
14 |
Correct |
302 ms |
780216 KB |
Output is correct |
15 |
Correct |
326 ms |
780292 KB |
Output is correct |
16 |
Correct |
339 ms |
780328 KB |
Output is correct |
17 |
Correct |
319 ms |
780312 KB |
Output is correct |
18 |
Correct |
335 ms |
780252 KB |
Output is correct |
19 |
Correct |
314 ms |
780252 KB |
Output is correct |
20 |
Correct |
328 ms |
780288 KB |
Output is correct |
21 |
Correct |
318 ms |
780296 KB |
Output is correct |
22 |
Correct |
301 ms |
780216 KB |
Output is correct |
23 |
Correct |
296 ms |
780268 KB |
Output is correct |
24 |
Correct |
285 ms |
780284 KB |
Output is correct |
25 |
Correct |
297 ms |
780280 KB |
Output is correct |
26 |
Correct |
290 ms |
780344 KB |
Output is correct |
27 |
Correct |
302 ms |
780368 KB |
Output is correct |
28 |
Correct |
303 ms |
780260 KB |
Output is correct |
29 |
Correct |
291 ms |
780280 KB |
Output is correct |
30 |
Correct |
315 ms |
780288 KB |
Output is correct |
31 |
Correct |
288 ms |
780196 KB |
Output is correct |
32 |
Correct |
291 ms |
780300 KB |
Output is correct |
33 |
Correct |
293 ms |
780312 KB |
Output is correct |
34 |
Correct |
288 ms |
780236 KB |
Output is correct |
35 |
Correct |
295 ms |
780364 KB |
Output is correct |
36 |
Correct |
292 ms |
780228 KB |
Output is correct |
37 |
Correct |
297 ms |
780316 KB |
Output is correct |
38 |
Correct |
291 ms |
780356 KB |
Output is correct |
39 |
Correct |
289 ms |
780236 KB |
Output is correct |
40 |
Correct |
313 ms |
780264 KB |
Output is correct |
41 |
Correct |
290 ms |
780196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
290 ms |
780216 KB |
Output is correct |
2 |
Correct |
292 ms |
780276 KB |
Output is correct |
3 |
Correct |
286 ms |
780312 KB |
Output is correct |
4 |
Correct |
294 ms |
780360 KB |
Output is correct |
5 |
Correct |
321 ms |
780220 KB |
Output is correct |
6 |
Correct |
294 ms |
780208 KB |
Output is correct |
7 |
Correct |
293 ms |
780296 KB |
Output is correct |
8 |
Correct |
289 ms |
780236 KB |
Output is correct |
9 |
Correct |
318 ms |
780216 KB |
Output is correct |
10 |
Correct |
299 ms |
780224 KB |
Output is correct |
11 |
Correct |
297 ms |
780340 KB |
Output is correct |
12 |
Correct |
285 ms |
780364 KB |
Output is correct |
13 |
Correct |
294 ms |
780364 KB |
Output is correct |
14 |
Correct |
297 ms |
780256 KB |
Output is correct |
15 |
Correct |
293 ms |
780408 KB |
Output is correct |
16 |
Correct |
293 ms |
780380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
369 ms |
780228 KB |
Output is correct |
2 |
Correct |
363 ms |
780276 KB |
Output is correct |
3 |
Correct |
326 ms |
780232 KB |
Output is correct |
4 |
Correct |
344 ms |
780216 KB |
Output is correct |
5 |
Correct |
300 ms |
780264 KB |
Output is correct |
6 |
Correct |
336 ms |
780252 KB |
Output is correct |
7 |
Correct |
304 ms |
780304 KB |
Output is correct |
8 |
Correct |
308 ms |
780204 KB |
Output is correct |
9 |
Correct |
360 ms |
780280 KB |
Output is correct |
10 |
Correct |
288 ms |
780272 KB |
Output is correct |
11 |
Correct |
302 ms |
780252 KB |
Output is correct |
12 |
Correct |
296 ms |
780260 KB |
Output is correct |
13 |
Correct |
307 ms |
780272 KB |
Output is correct |
14 |
Correct |
302 ms |
780216 KB |
Output is correct |
15 |
Correct |
326 ms |
780292 KB |
Output is correct |
16 |
Correct |
339 ms |
780328 KB |
Output is correct |
17 |
Correct |
319 ms |
780312 KB |
Output is correct |
18 |
Correct |
335 ms |
780252 KB |
Output is correct |
19 |
Correct |
314 ms |
780252 KB |
Output is correct |
20 |
Correct |
328 ms |
780288 KB |
Output is correct |
21 |
Correct |
318 ms |
780296 KB |
Output is correct |
22 |
Correct |
301 ms |
780216 KB |
Output is correct |
23 |
Correct |
296 ms |
780268 KB |
Output is correct |
24 |
Correct |
285 ms |
780284 KB |
Output is correct |
25 |
Correct |
297 ms |
780280 KB |
Output is correct |
26 |
Correct |
290 ms |
780344 KB |
Output is correct |
27 |
Correct |
302 ms |
780368 KB |
Output is correct |
28 |
Correct |
303 ms |
780260 KB |
Output is correct |
29 |
Correct |
291 ms |
780280 KB |
Output is correct |
30 |
Correct |
315 ms |
780288 KB |
Output is correct |
31 |
Correct |
288 ms |
780196 KB |
Output is correct |
32 |
Correct |
291 ms |
780300 KB |
Output is correct |
33 |
Correct |
293 ms |
780312 KB |
Output is correct |
34 |
Correct |
288 ms |
780236 KB |
Output is correct |
35 |
Correct |
295 ms |
780364 KB |
Output is correct |
36 |
Correct |
292 ms |
780228 KB |
Output is correct |
37 |
Correct |
297 ms |
780316 KB |
Output is correct |
38 |
Correct |
291 ms |
780356 KB |
Output is correct |
39 |
Correct |
289 ms |
780236 KB |
Output is correct |
40 |
Correct |
313 ms |
780264 KB |
Output is correct |
41 |
Correct |
290 ms |
780196 KB |
Output is correct |
42 |
Correct |
290 ms |
780216 KB |
Output is correct |
43 |
Correct |
292 ms |
780276 KB |
Output is correct |
44 |
Correct |
286 ms |
780312 KB |
Output is correct |
45 |
Correct |
294 ms |
780360 KB |
Output is correct |
46 |
Correct |
321 ms |
780220 KB |
Output is correct |
47 |
Correct |
294 ms |
780208 KB |
Output is correct |
48 |
Correct |
293 ms |
780296 KB |
Output is correct |
49 |
Correct |
289 ms |
780236 KB |
Output is correct |
50 |
Correct |
318 ms |
780216 KB |
Output is correct |
51 |
Correct |
299 ms |
780224 KB |
Output is correct |
52 |
Correct |
297 ms |
780340 KB |
Output is correct |
53 |
Correct |
285 ms |
780364 KB |
Output is correct |
54 |
Correct |
294 ms |
780364 KB |
Output is correct |
55 |
Correct |
297 ms |
780256 KB |
Output is correct |
56 |
Correct |
293 ms |
780408 KB |
Output is correct |
57 |
Correct |
293 ms |
780380 KB |
Output is correct |
58 |
Correct |
349 ms |
780236 KB |
Output is correct |
59 |
Correct |
309 ms |
780368 KB |
Output is correct |
60 |
Correct |
310 ms |
780376 KB |
Output is correct |
61 |
Correct |
302 ms |
780312 KB |
Output is correct |
62 |
Correct |
293 ms |
780268 KB |
Output is correct |
63 |
Correct |
297 ms |
780344 KB |
Output is correct |
64 |
Correct |
307 ms |
780420 KB |
Output is correct |
65 |
Correct |
308 ms |
780224 KB |
Output is correct |
66 |
Correct |
315 ms |
780320 KB |
Output is correct |
67 |
Correct |
305 ms |
780268 KB |
Output is correct |
68 |
Correct |
348 ms |
780236 KB |
Output is correct |
69 |
Correct |
313 ms |
780300 KB |
Output is correct |
70 |
Correct |
334 ms |
780228 KB |
Output is correct |
71 |
Correct |
316 ms |
780252 KB |
Output is correct |
72 |
Correct |
318 ms |
780248 KB |
Output is correct |
73 |
Correct |
299 ms |
780268 KB |
Output is correct |