#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define db double
#define ll __int128
//#define int long long
#define pb push_back
#define fs first
#define sd second
#define Mod long(1e9 + 7)
#define all(x) x.begin(), x.end()
#define unvisited long(-1)
#define Eps double(1e-9)
#define _for(i, n) for(int i = 0; i < (n); i++)
#define dbg(x) cout << #x ": " << x << endl;
const int Max = 1e6 + 7, Inf = 1e9 + 7;
void print(bool x) { cout << (x ? "YES" : "NO") << endl; }
string tostring (__int128 x)
{
string ans = "";
while(x > 0)
{
ans += (x % 10 + '0');
x /= 10;
}
reverse(all(ans));
return ans;
}
//int dp[70][70][70][3];
void solve()
{
int n; string s; cin >> n >> s;
vector <int> r, g, y, rs(n), gs(n), ys(n);
for(int i = 0; i < n; i++)
{
if(i != 0)
{
rs[i] = rs[i - 1];
gs[i] = gs[i - 1];
ys[i] = ys[i - 1];
}
if(s[i] == 'R') r.pb(i), rs[i]++;
if(s[i] == 'G') g.pb(i), gs[i]++;
if(s[i] == 'Y') y.pb(i), ys[i]++;
}
/*
for(int i = 0; i < n + 7; i++)
{
for(int j = 0; j < n + 7; j ++)
for(int k = 0; k < n + 7; k++)
dp[i][j][k][0] = dp[i][j][k][1] = dp[i][j][k][2] = Inf;
}*/
int rz = r.size(), gz = g.size(), yz = y.size();
vector <vector<vector<vector<int>>>> dp(rz + 1, vector <vector<vector<int>>> (gz + 1,
vector <vector<int>> (yz + 1, vector <int> (3, Inf))));
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= min(rz, i + 1); j++)
{
for(int k = 0; k + j <= i + 1 && k <= gz; k++)
{
//for(int l = 0; l + k + j <= i + 1 && l <= yz; l++)
//{
//if(j + k + l != i + 1) continue;
int l = (i + 1) - (j + k);
if(l > yz) continue;
if(j > 0)
{
int gx = 0, yx = 0, pj = r[j - 1];
gx = max(0, k - gs[pj]);
yx = max(0, l - ys[pj]);
pj += gx + yx;
dp[j][k][l][0] = min(dp[j][k][l][0], min(dp[j - 1][k][l][1], dp[j - 1][k][l][2]) + abs(pj - i));
}
if(k > 0)
{
int rx = 0, yx = 0, pk = g[k - 1];
rx = max(0, j - rs[pk]);
yx = max(0, l - ys[pk]);
pk += rx + yx;
dp[j][k][l][1] = min(dp[j][k][l][1], min(dp[j][k - 1][l][0], dp[j][k-1][l][2]) + abs(pk - i));
}
if(l > 0)
{
int rx = 0, gx = 0, pl = y[l - 1];//if(i == n - 1) dbg(pl);
rx = max(0, j - rs[pl]);
gx = max(0, k - gs[pl]);
pl += rx + gx;
dp[j][k][l][2] = min(dp[j][k][l][2], min(dp[j][k][l - 1][0], dp[j][k][l - 1][1]) + abs(pl - i));
}
//cout << i << " " << j << " " << k << " " << l << " " << endl;
// dbg(dp[j][k][l][0]); dbg(dp[j][k][l][1]); dbg(dp[j][k][l][2]);
//}
}
}
}
int ans = min(dp[rz][gz][yz][0], min(dp[rz][gz][yz][1], dp[rz][gz][yz][2]));
cout << (ans == Inf ? -1 : ans) << endl;
}
/*
5
RRGYY
*/
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int Q = 1; //cin >> Q;
while (Q--)
{
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 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 |
464 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 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 |
464 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
988 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
1 ms |
860 KB |
Output is correct |
38 |
Correct |
1 ms |
856 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
0 ms |
464 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
12 ms |
3960 KB |
Output is correct |
3 |
Correct |
16 ms |
3952 KB |
Output is correct |
4 |
Correct |
12 ms |
3932 KB |
Output is correct |
5 |
Correct |
13 ms |
3960 KB |
Output is correct |
6 |
Correct |
13 ms |
3932 KB |
Output is correct |
7 |
Correct |
12 ms |
3952 KB |
Output is correct |
8 |
Correct |
12 ms |
3928 KB |
Output is correct |
9 |
Correct |
17 ms |
3932 KB |
Output is correct |
10 |
Correct |
12 ms |
3968 KB |
Output is correct |
11 |
Correct |
12 ms |
3932 KB |
Output is correct |
12 |
Correct |
3 ms |
1372 KB |
Output is correct |
13 |
Correct |
5 ms |
1884 KB |
Output is correct |
14 |
Correct |
8 ms |
2860 KB |
Output is correct |
15 |
Correct |
15 ms |
3972 KB |
Output is correct |
16 |
Correct |
12 ms |
3932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 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 |
464 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
988 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
1 ms |
860 KB |
Output is correct |
38 |
Correct |
1 ms |
856 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
0 ms |
464 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
12 ms |
3960 KB |
Output is correct |
44 |
Correct |
16 ms |
3952 KB |
Output is correct |
45 |
Correct |
12 ms |
3932 KB |
Output is correct |
46 |
Correct |
13 ms |
3960 KB |
Output is correct |
47 |
Correct |
13 ms |
3932 KB |
Output is correct |
48 |
Correct |
12 ms |
3952 KB |
Output is correct |
49 |
Correct |
12 ms |
3928 KB |
Output is correct |
50 |
Correct |
17 ms |
3932 KB |
Output is correct |
51 |
Correct |
12 ms |
3968 KB |
Output is correct |
52 |
Correct |
12 ms |
3932 KB |
Output is correct |
53 |
Correct |
3 ms |
1372 KB |
Output is correct |
54 |
Correct |
5 ms |
1884 KB |
Output is correct |
55 |
Correct |
8 ms |
2860 KB |
Output is correct |
56 |
Correct |
15 ms |
3972 KB |
Output is correct |
57 |
Correct |
12 ms |
3932 KB |
Output is correct |
58 |
Correct |
246 ms |
133204 KB |
Output is correct |
59 |
Correct |
203 ms |
133416 KB |
Output is correct |
60 |
Correct |
212 ms |
132692 KB |
Output is correct |
61 |
Correct |
205 ms |
133704 KB |
Output is correct |
62 |
Correct |
22 ms |
12388 KB |
Output is correct |
63 |
Correct |
34 ms |
20748 KB |
Output is correct |
64 |
Correct |
111 ms |
66640 KB |
Output is correct |
65 |
Correct |
165 ms |
95760 KB |
Output is correct |
66 |
Correct |
202 ms |
131924 KB |
Output is correct |
67 |
Correct |
202 ms |
131928 KB |
Output is correct |
68 |
Correct |
221 ms |
134152 KB |
Output is correct |
69 |
Correct |
203 ms |
134736 KB |
Output is correct |
70 |
Correct |
199 ms |
134224 KB |
Output is correct |
71 |
Correct |
205 ms |
130952 KB |
Output is correct |
72 |
Correct |
54 ms |
34132 KB |
Output is correct |
73 |
Correct |
11 ms |
10588 KB |
Output is correct |