//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,abm,popcnt,mmx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef complex<double> cd;
constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007;
constexpr db PI = acos(-1);
constexpr bool IS_FILE = false, IS_TEST_CASES = false;
random_device rd;
mt19937 rnd32(rd());
mt19937_64 rnd64(rd());
template<typename T>
bool assign_max(T& a, T b) {
if (b > a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool assign_min(T& a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
T square(T a) {
return a * a;
}
template<>
struct std::hash<pair<ll, ll>> {
ll operator() (pair<ll, ll> p) const {
return ((__int128)p.first * MOD + p.second) % INF64;
}
};
void solve() {
ll n;
cin >> n;
string s;
cin >> s;
vector<ll> pr(1, 0), pg(1, 0), py(1, 0);
vector<ll> posr, posg, posy;
for (ll i = 0; i < n; i++) {
pr.push_back(pr.back() + (s[i] == 'R'));
pg.push_back(pg.back() + (s[i] == 'G'));
py.push_back(py.back() + (s[i] == 'Y'));
if (s[i] == 'R') {
posr.push_back(i);
}
if (s[i] == 'G') {
posg.push_back(i);
}
if (s[i] == 'Y') {
posy.push_back(i);
}
}
vector<vector<vector<vector<ll>>>> dp(cy.back() + 1, vector<vector<vector<ll>>>(pr.back() + 1, vector<vector<ll>>(pg.back() + 1, vector<ll>(3, INF32))));
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for (ll cy = 0; cy <= py.back(); cy++) {
for (ll cr = 0; cr <= pr.back(); cr++) {
for (ll cg = 0; cg <= pg.back(); cg++) {
for (ll lst = 0; lst < 3; lst++) {
if ((lst == 0 && cr == 0) || (lst == 1 && cg == 0) || (lst == 2 && cy == 0)) {
continue;
}
for (ll olst = 0; olst < 3; olst++) {
if (olst == lst) {
continue;
}
ll add = 0;
if (lst == 0) {
add = max(0ll, pg[posr[cr - 1]] - cg) + max(0ll, py[posr[cr - 1]] - cy);
}
if (lst == 1) {
add = max(0ll, pr[posg[cg - 1]] - cr) + max(0ll, py[posg[cg - 1]] - cy);
}
if (lst == 2) {
add = max(0ll, pr[posy[cy - 1]] - cr) + max(0ll, pg[posy[cy - 1]] - cg);
}
assign_min(dp[cy][cr][cg][lst], dp[cy - (lst == 2)][cr - (lst == 0)][cg - (lst == 1)][olst] + add);
}
}
}
}
}
ll ans = min({dp[py.back()][pr.back()][pg.back()][0], dp[py.back()][pr.back()][pg.back()][1], dp[py.back()][pr.back()][pg.back()][2]});
cout << (ans == INF32 ? -1 : ans) << '\n';
}
int main() {
if (IS_FILE) {
freopen("", "r", stdin);
freopen("", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
if (IS_TEST_CASES) {
cin >> t;
}
for (ll i = 0; i < t; i++) {
solve();
}
}
Compilation message
joi2019_ho_t3.cpp: In function 'void solve()':
joi2019_ho_t3.cpp:71:47: error: 'cy' was not declared in this scope; did you mean 'py'?
71 | vector<vector<vector<vector<ll>>>> dp(cy.back() + 1, vector<vector<vector<ll>>>(pr.back() + 1, vector<vector<ll>>(pg.back() + 1, vector<ll>(3, INF32))));
| ^~
| py
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:108:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | freopen("", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:109:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | freopen("", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~