#include <bits/stdc++.h>
// #include <ext/rope>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
using namespace std;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define bit(mask, i) ((mask >> i) & 1)
#define el '\n'
#define F first
#define S second
template <class X, class Y> bool maximize(X &x, const Y &y) { return (x < y ? x = y, 1 : 0); }
template <class X, class Y> bool minimize(X &x, const Y &y) { return (x > y ? x = y, 1 : 0); }
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;
const int MULTI = 0;
const ld eps = 1e-9;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; // R D L U
const int ddx[4] = {-1, 1, 1, -1}, ddy[4] = {1, 1, -1, -1}; // UR DR DL UL
const char cx[4] = {'R', 'D', 'L', 'U'};
const ll base = 31;
const int nMOD = 2;
const ll mods[] = {(ll)1e9 + 10777, (ll)1e9 + 19777, (ll)1e9 + 3, (ll)1e9 + 3777};
const int N = 4e2 + 5;
int n, dp[N][N][N][3], pref[3][N], pos[3][N], cnt[3];
string s;
void solve() {
cin >> n >> s;
for (auto &c: s)
if (c == 'R')
c = 'A';
else if (c == 'G')
c = 'B';
else
c = 'C';
s = ' ' + s;
for (int i = 1; i <= n; ++i)
for (int j = 0; j < 3; ++j) {
pref[j][i] = pref[j][i - 1];
if (s[i] - 'A' == j) {
pref[j][i]++;
pos[j][++cnt[j]] = i;
}
}
memset(dp, 0x3f, sizeof(dp));
for (int i = 0; i < 3; ++i)
dp[0][0][0][i] = 0;
for (int i = 0; i <= cnt[0]; ++i)
for (int j = 0; j <= cnt[1]; ++j)
for (int k = 0; k <= cnt[2]; ++k)
for (int pre = 0; pre < 3; ++pre)
for (int nxt = 0; nxt < 3; ++nxt) {
if (pre == nxt) continue;
if ((nxt == 0 && i == cnt[0]) || (nxt == 1 && j == cnt[1]) || (nxt == 2 && k == cnt[2])) continue;
int posPre, posNxt, posOth, oth;
if (pre == 0) {
posPre = pos[0][i];
if (nxt == 1) posNxt = pos[1][j + 1], posOth = pos[2][k], oth = 2;
else posNxt = pos[2][k + 1], posOth = pos[1][j], oth = 1;
}
else if (pre == 1) {
posPre = pos[1][j];
if (nxt == 0) posNxt = pos[0][i + 1], posOth = pos[2][k], oth = 2;
else posNxt = pos[2][k + 1], posOth = pos[0][i], oth = 0;
}
else {
posPre = pos[2][k];
if (nxt == 0) posNxt = pos[0][i + 1], posOth = pos[1][j], oth = 1;
else posNxt = pos[1][j + 1], posOth = pos[0][i], oth = 0;
}
int tmp = 0;
tmp += max(0, pref[pre][posNxt] - pref[pre][posPre]);
tmp += max(0, pref[oth][posNxt] - pref[oth][posOth]);
tmp += dp[i][j][k][pre];
if (nxt == 0)
minimize(dp[i + 1][j][k][nxt], tmp);
else if (nxt == 1)
minimize(dp[i][j + 1][k][nxt], tmp);
else
minimize(dp[i][j][k + 1][nxt], tmp);
}
int res = INF;
for (int i = 0; i < 3; ++i)
minimize(res, dp[cnt[0]][cnt[1]][cnt[2]][i]);
cout << (res == INF ? -1 : res);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (!MULTI) solve();
else {
int test; cin >> test;
while (test--) solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |