#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]++;
}
int rz = r.size(), gz = g.size(), yz = y.size();
int mx = 0; mx = max(rz, max(gz, yz));
if(rz == mx)
{
swap(r, y);
swap(rs, ys);
swap(rz, yz);
}
if(gz == mx)
{
swap(g, y);
swap(gs, ys);
swap(gz, yz);
}
/*
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;
}*/
vector <vector<vector<vector<int>>>> dp(rz + 1, vector <vector<vector<int>>> ());
// vector <vector<vector<vector<int>>>> dp(rz + 1, vector <vector<vector<int>>> (gz + 1,
// vector <vector<int>> (yz + 1, vector <int> (3, Inf))));
for(int i = 0; i < rz + 1; i++)
{
dp[i].assign(min(gz + 1, n - i + 1), vector <vector<int>> ());
for(int j = 0; j < min(gz + 1, n - i + 1); j++)
{
dp[i][j].assign(min(yz + 1, n - (i + j) + 1), vector <int> (3, Inf));
}
}
//cout << "WA";
//return;
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0; int val;
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= min(rz, i + 1); j++)
{
for(int k = max(0, (i + 1) - (yz + j)); 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)
{
val = r[j - 1] + max(0, k - gs[r[j - 1]]) + max(0, l - ys[r[j - 1]]);
dp[j][k][l][0] = min(dp[j - 1][k][l][1], dp[j - 1][k][l][2]) + abs(val - i);
}
if(k > 0)
{
val = g[k - 1] + max(0, j - rs[g[k - 1]]) + max(0, l - ys[g[k - 1]]);
dp[j][k][l][1] = min(dp[j][k - 1][l][0], dp[j][k-1][l][2]) + abs(val - i);
}
if(l > 0)
{
val = y[l - 1] + max(0, j - rs[y[l - 1]]) + max(0, k - gs[y[l - 1]]);
dp[j][k][l][2] = min(dp[j][k][l - 1][0], dp[j][k][l - 1][1]) + abs(val - 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 |
344 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 |
344 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 |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
1 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 |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
344 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 |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
1 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 |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
856 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
2 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 |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
856 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 |
796 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 |
856 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
348 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 |
1012 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
0 ms |
344 KB |
Output is correct |
41 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
2684 KB |
Output is correct |
3 |
Correct |
3 ms |
2652 KB |
Output is correct |
4 |
Correct |
3 ms |
2536 KB |
Output is correct |
5 |
Correct |
3 ms |
2680 KB |
Output is correct |
6 |
Correct |
3 ms |
2684 KB |
Output is correct |
7 |
Correct |
3 ms |
2452 KB |
Output is correct |
8 |
Correct |
3 ms |
2652 KB |
Output is correct |
9 |
Correct |
3 ms |
2652 KB |
Output is correct |
10 |
Correct |
3 ms |
2652 KB |
Output is correct |
11 |
Correct |
3 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
2 ms |
1884 KB |
Output is correct |
15 |
Correct |
3 ms |
2652 KB |
Output is correct |
16 |
Correct |
3 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
344 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 |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
1 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 |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
856 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
2 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 |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
856 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 |
796 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 |
856 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
348 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 |
1012 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
0 ms |
344 KB |
Output is correct |
41 |
Correct |
0 ms |
604 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
3 ms |
2684 KB |
Output is correct |
44 |
Correct |
3 ms |
2652 KB |
Output is correct |
45 |
Correct |
3 ms |
2536 KB |
Output is correct |
46 |
Correct |
3 ms |
2680 KB |
Output is correct |
47 |
Correct |
3 ms |
2684 KB |
Output is correct |
48 |
Correct |
3 ms |
2452 KB |
Output is correct |
49 |
Correct |
3 ms |
2652 KB |
Output is correct |
50 |
Correct |
3 ms |
2652 KB |
Output is correct |
51 |
Correct |
3 ms |
2652 KB |
Output is correct |
52 |
Correct |
3 ms |
2652 KB |
Output is correct |
53 |
Correct |
1 ms |
860 KB |
Output is correct |
54 |
Correct |
1 ms |
1372 KB |
Output is correct |
55 |
Correct |
2 ms |
1884 KB |
Output is correct |
56 |
Correct |
3 ms |
2652 KB |
Output is correct |
57 |
Correct |
3 ms |
2652 KB |
Output is correct |
58 |
Correct |
242 ms |
132268 KB |
Output is correct |
59 |
Correct |
201 ms |
132508 KB |
Output is correct |
60 |
Correct |
202 ms |
131348 KB |
Output is correct |
61 |
Correct |
193 ms |
132760 KB |
Output is correct |
62 |
Correct |
14 ms |
11096 KB |
Output is correct |
63 |
Correct |
30 ms |
19544 KB |
Output is correct |
64 |
Correct |
112 ms |
65364 KB |
Output is correct |
65 |
Correct |
158 ms |
94304 KB |
Output is correct |
66 |
Correct |
193 ms |
130696 KB |
Output is correct |
67 |
Correct |
219 ms |
130708 KB |
Output is correct |
68 |
Correct |
196 ms |
132740 KB |
Output is correct |
69 |
Correct |
199 ms |
133664 KB |
Output is correct |
70 |
Correct |
194 ms |
133204 KB |
Output is correct |
71 |
Correct |
224 ms |
130132 KB |
Output is correct |
72 |
Correct |
56 ms |
33872 KB |
Output is correct |
73 |
Correct |
10 ms |
9016 KB |
Output is correct |