#include <bits/stdc++.h>
#define int short
#define debug cout << "ok\n";
#define SQR(x) (1LL * ((x) * (x)))
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pli pair<ll,int>
#define vi vector<int>
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef unsigned int ui;
using namespace std;
const int M = 1e9 + 7;
const int INF = 1e9 + 7;
const ll INFLL = (ll)2e18 + 7LL;
const ld PI = acos(-1);
const int dx[] = {1, -1, 0, 0, -1, 1, 1, -1};
const int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};
template<class _, class __>
bool minimize(_ &x, const __ y){
if(x > y){
x = y;
return true;
} else return false;
}
template<class _, class __>
bool maximize(_ &x, const __ y){
if(x < y){
x = y;
return true;
} else return false;
}
template<class _,class __>
void Add(_ &x, const __ y) {
x += y;
if (x >= M) {
x -= M;
}
return;
}
template<class _,class __>
void Diff(_ &x, const __ y) {
x -= y;
if (x < 0) {
x += M;
}
return;
}
//--------------------------------------------------------------
const int MaxN = 407;
int n,f[MaxN][MaxN][MaxN][3],g[3][MaxN][3][MaxN];
vi lis[3];
void sol() {
cin >> n;
for (int i=0;i<3;i++) lis[i].pb(0);
for (int i=1;i<=n;i++) {
char x;
cin >> x;
int xx;
if (x == 'R') xx = 0;
else if (x == 'G') xx = 1;
else xx = 2;
lis[xx].pb(i);
}
for (int i=0;i<3;i++) {
for (int j=1;j<lis[i].size();j++) {
for (int k=0;k<3;k++) {
for (int z=1;z<lis[k].size();z++) {
g[i][j][k][z] = g[i][j][k][z-1];
if (lis[k][z] > lis[i][j]) g[i][j][k][z]++;
}
}
}
}
memset(f,0x3f,sizeof(f));
f[0][0][0][0] = 0;
f[0][0][0][1] = 0;
f[0][0][0][2] = 0;
for (int i=0;i<lis[0].size();i++) {
for (int j=0;j<lis[1].size();j++) {
for (int k=0;k<lis[2].size();k++) {
minimize(f[i+1][j][k][0],f[i][j][k][1] + g[0][i+1][1][j] + g[0][i+1][2][k]);
minimize(f[i+1][j][k][0],f[i][j][k][2] + g[0][i+1][1][j] + g[0][i+1][2][k]);
minimize(f[i][j+1][k][1],f[i][j][k][0] + g[1][j+1][0][i] + g[1][j+1][2][k]);
minimize(f[i][j+1][k][1],f[i][j][k][2] + g[1][j+1][0][i] + g[1][j+1][2][k]);
minimize(f[i][j][k+1][2],f[i][j][k][0] + g[2][k+1][0][i] + g[2][k+1][1][j]);
minimize(f[i][j][k+1][2],f[i][j][k][1] + g[2][k+1][0][i] + g[2][k+1][1][j]);
}
}
}
int res = 700;
for (int i=0;i<3;i++) minimize(res,f[(int)lis[0].size()-1][(int)lis[1].size()-1][(int)lis[2].size()-1][i]);
if (res >= 700) cout << -1;
else cout << res;
}
signed main() {
// freopen("test.inp","r",stdin);
// freopen("test.out","w",stdout);
FAST
int t=1;
// cin >> t;
while (t--) sol();
}
Compilation message (stderr)
joi2019_ho_t3.cpp:27:19: warning: overflow in conversion from 'double' to 'short int' changes value from '1.000000007e+9' to '32767' [-Woverflow]
27 | const int M = 1e9 + 7;
| ~~~~^~~
joi2019_ho_t3.cpp:28:21: warning: overflow in conversion from 'double' to 'short int' changes value from '1.000000007e+9' to '32767' [-Woverflow]
28 | const int INF = 1e9 + 7;
| ~~~~^~~| # | 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... |