#include<iostream>
#include<iomanip>
#include<algorithm>
#include<stack>
#include<queue>
#include<string>
#include<string.h>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<cstdio>
#include<bitset>
#include<chrono>
#include<random>
#include<ext/rope>
/* ordered_set
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
*/
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 400 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e8;
int n;
string s;
int f[maxN][maxN][maxN][3];
int cntA[maxN][3], cntB[maxN][3], cntC[maxN][3];
void ReadInput()
{
cin >> n;
cin >> s;
s = ' ' + s;
}
void Solve()
{
int cnt1 = 0, cnt2 = 0, cnt3 = 0;
for(int i=1; i<=n; i++)
{
if(s[i] == 'R')
{
cnt1++;
cntA[cnt1][1] = cnt2;
cntA[cnt1][2] = cnt3;
}
if(s[i] == 'G')
{
cnt2++;
cntB[cnt2][0] = cnt1;
cntB[cnt2][2] = cnt3;
}
if(s[i] == 'Y')
{
cnt3++;
cntC[cnt3][0] = cnt1;
cntC[cnt3][1] = cnt2;
}
}
memset(f, 37, sizeof f);
f[0][0][0][0] = f[0][0][0][1] = f[0][0][0][2] = 0;
for(int i=0; i<=cnt1; i++)
for(int j=0; j<=cnt2; j++)
for(int k=0; k<=cnt3; k++)
{
if(i < cnt1) f[i + 1][j][k][0] = min(f[i + 1][j][k][0], min(f[i][j][k][1], f[i][j][k][2]) + abs(cntA[i + 1][1] - j) + abs(cntA[i + 1][2] - k));
if(j < cnt2) f[i][j + 1][k][1] = min(f[i][j + 1][k][1], min(f[i][j][k][0], f[i][j][k][2]) + abs(cntB[j + 1][0] - i) + abs(cntB[j + 1][2] - k));
if(k < cnt3) f[i][j][k + 1][2] = min(f[i][j][k + 1][2], min(f[i][j][k][0], f[i][j][k][1]) + abs(cntC[k + 1][0] - i) + abs(cntC[k + 1][1] - j));
}
// cout << f[2][1][0][0];return;
int res = min({f[cnt1][cnt2][cnt3][0], f[cnt1][cnt2][cnt3][1], f[cnt1][cnt2][cnt3][2]}) / 2;
cout << (res < oo ? res : -1);
}
int32_t main()
{
//freopen("x.inp", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ReadInput();
Solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
460 ms |
780308 KB |
Output is correct |
2 |
Correct |
335 ms |
780288 KB |
Output is correct |
3 |
Correct |
307 ms |
780300 KB |
Output is correct |
4 |
Correct |
371 ms |
780348 KB |
Output is correct |
5 |
Correct |
302 ms |
780312 KB |
Output is correct |
6 |
Correct |
321 ms |
780256 KB |
Output is correct |
7 |
Correct |
312 ms |
780256 KB |
Output is correct |
8 |
Correct |
312 ms |
780204 KB |
Output is correct |
9 |
Correct |
328 ms |
780364 KB |
Output is correct |
10 |
Correct |
308 ms |
780196 KB |
Output is correct |
11 |
Correct |
317 ms |
780292 KB |
Output is correct |
12 |
Correct |
315 ms |
780196 KB |
Output is correct |
13 |
Correct |
320 ms |
780252 KB |
Output is correct |
14 |
Correct |
321 ms |
780240 KB |
Output is correct |
15 |
Correct |
324 ms |
780312 KB |
Output is correct |
16 |
Correct |
356 ms |
780304 KB |
Output is correct |
17 |
Correct |
308 ms |
780252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
460 ms |
780308 KB |
Output is correct |
2 |
Correct |
335 ms |
780288 KB |
Output is correct |
3 |
Correct |
307 ms |
780300 KB |
Output is correct |
4 |
Correct |
371 ms |
780348 KB |
Output is correct |
5 |
Correct |
302 ms |
780312 KB |
Output is correct |
6 |
Correct |
321 ms |
780256 KB |
Output is correct |
7 |
Correct |
312 ms |
780256 KB |
Output is correct |
8 |
Correct |
312 ms |
780204 KB |
Output is correct |
9 |
Correct |
328 ms |
780364 KB |
Output is correct |
10 |
Correct |
308 ms |
780196 KB |
Output is correct |
11 |
Correct |
317 ms |
780292 KB |
Output is correct |
12 |
Correct |
315 ms |
780196 KB |
Output is correct |
13 |
Correct |
320 ms |
780252 KB |
Output is correct |
14 |
Correct |
321 ms |
780240 KB |
Output is correct |
15 |
Correct |
324 ms |
780312 KB |
Output is correct |
16 |
Correct |
356 ms |
780304 KB |
Output is correct |
17 |
Correct |
308 ms |
780252 KB |
Output is correct |
18 |
Correct |
320 ms |
780260 KB |
Output is correct |
19 |
Correct |
307 ms |
780320 KB |
Output is correct |
20 |
Correct |
324 ms |
780352 KB |
Output is correct |
21 |
Correct |
305 ms |
780280 KB |
Output is correct |
22 |
Correct |
308 ms |
780312 KB |
Output is correct |
23 |
Correct |
311 ms |
780288 KB |
Output is correct |
24 |
Correct |
302 ms |
780220 KB |
Output is correct |
25 |
Correct |
307 ms |
780280 KB |
Output is correct |
26 |
Correct |
310 ms |
780224 KB |
Output is correct |
27 |
Correct |
313 ms |
780364 KB |
Output is correct |
28 |
Correct |
313 ms |
780248 KB |
Output is correct |
29 |
Correct |
308 ms |
780252 KB |
Output is correct |
30 |
Correct |
317 ms |
780272 KB |
Output is correct |
31 |
Correct |
314 ms |
780288 KB |
Output is correct |
32 |
Correct |
340 ms |
780228 KB |
Output is correct |
33 |
Correct |
308 ms |
780364 KB |
Output is correct |
34 |
Correct |
308 ms |
780272 KB |
Output is correct |
35 |
Correct |
308 ms |
780232 KB |
Output is correct |
36 |
Correct |
300 ms |
780236 KB |
Output is correct |
37 |
Correct |
305 ms |
780300 KB |
Output is correct |
38 |
Correct |
317 ms |
780240 KB |
Output is correct |
39 |
Correct |
310 ms |
780364 KB |
Output is correct |
40 |
Correct |
310 ms |
780240 KB |
Output is correct |
41 |
Correct |
318 ms |
780240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
308 ms |
780268 KB |
Output is correct |
2 |
Correct |
303 ms |
780288 KB |
Output is correct |
3 |
Correct |
309 ms |
780228 KB |
Output is correct |
4 |
Correct |
302 ms |
780276 KB |
Output is correct |
5 |
Correct |
304 ms |
780248 KB |
Output is correct |
6 |
Correct |
302 ms |
780256 KB |
Output is correct |
7 |
Correct |
301 ms |
780276 KB |
Output is correct |
8 |
Correct |
311 ms |
780320 KB |
Output is correct |
9 |
Correct |
302 ms |
780236 KB |
Output is correct |
10 |
Correct |
308 ms |
780396 KB |
Output is correct |
11 |
Correct |
304 ms |
780304 KB |
Output is correct |
12 |
Correct |
300 ms |
780236 KB |
Output is correct |
13 |
Correct |
303 ms |
780324 KB |
Output is correct |
14 |
Correct |
298 ms |
780312 KB |
Output is correct |
15 |
Correct |
302 ms |
780364 KB |
Output is correct |
16 |
Correct |
308 ms |
780244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
460 ms |
780308 KB |
Output is correct |
2 |
Correct |
335 ms |
780288 KB |
Output is correct |
3 |
Correct |
307 ms |
780300 KB |
Output is correct |
4 |
Correct |
371 ms |
780348 KB |
Output is correct |
5 |
Correct |
302 ms |
780312 KB |
Output is correct |
6 |
Correct |
321 ms |
780256 KB |
Output is correct |
7 |
Correct |
312 ms |
780256 KB |
Output is correct |
8 |
Correct |
312 ms |
780204 KB |
Output is correct |
9 |
Correct |
328 ms |
780364 KB |
Output is correct |
10 |
Correct |
308 ms |
780196 KB |
Output is correct |
11 |
Correct |
317 ms |
780292 KB |
Output is correct |
12 |
Correct |
315 ms |
780196 KB |
Output is correct |
13 |
Correct |
320 ms |
780252 KB |
Output is correct |
14 |
Correct |
321 ms |
780240 KB |
Output is correct |
15 |
Correct |
324 ms |
780312 KB |
Output is correct |
16 |
Correct |
356 ms |
780304 KB |
Output is correct |
17 |
Correct |
308 ms |
780252 KB |
Output is correct |
18 |
Correct |
320 ms |
780260 KB |
Output is correct |
19 |
Correct |
307 ms |
780320 KB |
Output is correct |
20 |
Correct |
324 ms |
780352 KB |
Output is correct |
21 |
Correct |
305 ms |
780280 KB |
Output is correct |
22 |
Correct |
308 ms |
780312 KB |
Output is correct |
23 |
Correct |
311 ms |
780288 KB |
Output is correct |
24 |
Correct |
302 ms |
780220 KB |
Output is correct |
25 |
Correct |
307 ms |
780280 KB |
Output is correct |
26 |
Correct |
310 ms |
780224 KB |
Output is correct |
27 |
Correct |
313 ms |
780364 KB |
Output is correct |
28 |
Correct |
313 ms |
780248 KB |
Output is correct |
29 |
Correct |
308 ms |
780252 KB |
Output is correct |
30 |
Correct |
317 ms |
780272 KB |
Output is correct |
31 |
Correct |
314 ms |
780288 KB |
Output is correct |
32 |
Correct |
340 ms |
780228 KB |
Output is correct |
33 |
Correct |
308 ms |
780364 KB |
Output is correct |
34 |
Correct |
308 ms |
780272 KB |
Output is correct |
35 |
Correct |
308 ms |
780232 KB |
Output is correct |
36 |
Correct |
300 ms |
780236 KB |
Output is correct |
37 |
Correct |
305 ms |
780300 KB |
Output is correct |
38 |
Correct |
317 ms |
780240 KB |
Output is correct |
39 |
Correct |
310 ms |
780364 KB |
Output is correct |
40 |
Correct |
310 ms |
780240 KB |
Output is correct |
41 |
Correct |
318 ms |
780240 KB |
Output is correct |
42 |
Correct |
308 ms |
780268 KB |
Output is correct |
43 |
Correct |
303 ms |
780288 KB |
Output is correct |
44 |
Correct |
309 ms |
780228 KB |
Output is correct |
45 |
Correct |
302 ms |
780276 KB |
Output is correct |
46 |
Correct |
304 ms |
780248 KB |
Output is correct |
47 |
Correct |
302 ms |
780256 KB |
Output is correct |
48 |
Correct |
301 ms |
780276 KB |
Output is correct |
49 |
Correct |
311 ms |
780320 KB |
Output is correct |
50 |
Correct |
302 ms |
780236 KB |
Output is correct |
51 |
Correct |
308 ms |
780396 KB |
Output is correct |
52 |
Correct |
304 ms |
780304 KB |
Output is correct |
53 |
Correct |
300 ms |
780236 KB |
Output is correct |
54 |
Correct |
303 ms |
780324 KB |
Output is correct |
55 |
Correct |
298 ms |
780312 KB |
Output is correct |
56 |
Correct |
302 ms |
780364 KB |
Output is correct |
57 |
Correct |
308 ms |
780244 KB |
Output is correct |
58 |
Correct |
326 ms |
780488 KB |
Output is correct |
59 |
Correct |
327 ms |
780452 KB |
Output is correct |
60 |
Correct |
323 ms |
780224 KB |
Output is correct |
61 |
Correct |
325 ms |
780232 KB |
Output is correct |
62 |
Correct |
308 ms |
780340 KB |
Output is correct |
63 |
Correct |
311 ms |
780428 KB |
Output is correct |
64 |
Correct |
316 ms |
780272 KB |
Output is correct |
65 |
Correct |
323 ms |
780448 KB |
Output is correct |
66 |
Correct |
324 ms |
780328 KB |
Output is correct |
67 |
Correct |
328 ms |
780268 KB |
Output is correct |
68 |
Correct |
328 ms |
780252 KB |
Output is correct |
69 |
Correct |
325 ms |
780236 KB |
Output is correct |
70 |
Correct |
327 ms |
780220 KB |
Output is correct |
71 |
Correct |
322 ms |
780296 KB |
Output is correct |
72 |
Correct |
308 ms |
780216 KB |
Output is correct |
73 |
Correct |
313 ms |
780244 KB |
Output is correct |