#include <algorithm> #include <iostream> #include <cstring> #include <iomanip> #include <cstdlib> #include <climits> #include <cstdio> #include <bitset> #include <string> #include <vector> #include <cctype> #include <stack> #include <queue> #include <deque> #include <cmath> #include <map> #include <set>
#define I inline #define fi first #define se second #define pb push_back #define MP make_pair #define PII pair<int , int> #define PSL pair<string , long long> #define PLL pair<long long , long long> #define all(x) (x).begin() , (x).end() #define copy(a , b) memcpy(a , b , sizeof(b)) #define clean(a , b) memset(a , b , sizeof(a)) #define rep(i , l , r) for (int i = (l); i <= (r); i ++) #define per(i , r , l) for (int i = (r); i >= (l); i --) #define PE(i , x) for(int i = head[x]; i; i = edge[i].last) #define DEBUG(x) std :: cerr << #x << '=' << x << std :: endl
typedef long long ll; typedef unsigned long long ull;
template <class T> inline void read(T &x) { char c = getchar(), f = 0; x = 0; while (!isdigit(c)) f = (c == '-'), c = getchar(); while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x = f ? -x : x; }
using namespace std;
const int N = 10000 + 5; const int M = 405 + 5; const int HA = 998244353;
int n , s[M] , pla[3][M] , tot[3]; char str[M];
int g[3][3][M][M] , f[M][M][M][3];
int cost(int col , int x , int y , int z) { int res = 0 , tp = 0; rep(i , 0 , 2) { if(i != col) { tp ++; if(tp & 1) res += g[col][i][x][y]; else res += g[col][i][x][z]; } } return res; }
void upd(int &x , int y) { x = min(x , y); }
#define LOCAL
int main() { #ifdef LOCAL freopen("b.in" , "r" , stdin); freopen("b.out" , "w" , stdout); #endif read(n); scanf("%s" , str + 1); rep(i , 1 , n) { if(str[i] == 'R') s[i] = 0; if(str[i] == 'G') s[i] = 1; if(str[i] == 'Y') s[i] = 2; pla[s[i]][++ tot[s[i]]] = i; } rep(op , 0 , 2) { rep(op2 , 0 , 2) { if(op == op2) continue; rep(i , 1 , tot[op]){ rep(j , 1 , tot[op2]) { g[op][op2][i][j] = g[op][op2][i][j - 1] + (pla[op][i] < pla[op2][j]); } } } } clean(f , 0x3f); int ans = 0x3f3f3f3f; rep(i , 0 , 2) f[0][0][0][i] = 0; rep(i , 0 , tot[0]) { rep(j , 0 , tot[1]) { rep(k , 0 , tot[2]) { if(i) rep(sta , 0 , 2) if(sta != 0) upd(f[i][j][k][0] , f[i - 1][j][k][sta] + cost(0 , i , j , k)); if(j) rep(sta , 0 , 2) if(sta != 1) upd(f[i][j][k][1] , f[i][j - 1][k][sta] + cost(1 , j , i , k)); if(k) rep(sta , 0 , 2) if(sta != 2) upd(f[i][j][k][2] , f[i][j][k - 1][sta] + cost(2 , k , i , j)); } } } rep(i , 0 , 2) ans = min(ans , f[tot[0]][tot[1]][tot[2]][i]); printf("%d\n" , ans == 0x3f3f3f3f ? -1 : ans); return 0; }
|