#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define ld long double
#define pb push_back
#define endl '\n'
#define all(v) v.begin(), v.end()
#define fr(m,n) for(int i=m;i<n;i++)
#define frr(m,n) for(int i=m-1;i>=n;i--)
#define ffr1 for(int i=n;i>=1;i--)
#define ffr for(int i=n-1;i>=0;i--)
#define ff for(int i=0;i<n;i++)
#define ff1 for(int i=1;i<=n;i++)
#define frj(m,n) for(int j=m;j<n;j++)
#define frl(m,n) for(int l=m;l<n;l++)
#define frd(m,n) for(int d=m;d<n;d++)
#define yes cout << "YES"
#define no cout << "NO"
#define yesm cout << "Yes"
#define nom cout << "No"
#define inf LLONG_MAX
#define ext {cout << -1; return;}
#define zxt {cout << 0; return;}
#define noxt {no;return;}
#define yesxt {yes;return;}
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define rall(a) a.rbegin(),a.rend()
#define lb lower_bound
#define ub upper_bound
#define pi pair<int,int>
#define vi vector<int>
#define vvi vector<vi>
#define vc vector<char>
#define vb vector<bool>
#define vs vector<string>
#define vpi vector<pi>
#define pq priority_queue
#define mii map<int,int>
#define mivi map<int,vi>
#define mci map<char,int>
#define als(i) cout << i.fi << " " << i.se << endl
#define si set<int>
#define msi multiset<int>
#define fi first
#define wq while(q--)
#define se second
#define sz size()
#define el cout<<endl
bool id = 0, id1 = 0, id2 = 0;
int x = 0, y = 0, z = 0, tp;
int ans = 0, num = 0, sum = 0, mo = 0, me = 0, cnt = 0, mi = inf, ma = 0;
string s = "", p = "", st = "";
int h = 0, w = 0, n = 0, m = 0, t = 0, k = 0, i = 0, j = 0, l = 0, r = 0, q = 0, a = 0, b = 0, c = 0, d = 0;
// const int mod = 1e9 + 7;
const int mod = 998244353;
ll fac(ll n) {ll ans = 1; for (int i = 1; i <= n; ++i) {ans = ans * i;} return ans;}
int binpow(int x, int y) {if (y == 0) return 1; int z = 1; while (y) { if (y & 1) z = z * x % mod; x = x * x % mod; y >>= 1;} return z;}
int inv(int x) {return binpow(x, mod - 2);}
int bintoint(string s) {reverse(all(s)); int xx = 0; int m = 1; int z = s.sz; frj(0, z) {xx += (s[j] == '1' ? m : 0); m <<= 1;} return xx;}
string inttobin(int x) {if (x == 0) return "0"; string s; while (x) {s += (x & 1) + '0'; x >>= 1;} reverse(all(s)); return s;}
int add(int x, int y) {int zz = ((x + y) % mod + mod) % mod; return zz;}
string ads(string s, string p) {if (s.sz > p.sz)swap(s, p); int n = s.sz, m = p.sz; reverse(all(s)); reverse(all(p)); int tm = 0; string st; fr(0, n) { int k = s[i] - '0' + p[i] - '0' + tm; st += ('0' + k % 10); tm = k / 10;} fr(n, m) { int k = p[i] - '0' + tm; st += ('0' + k % 10); tm = k / 10;} if (tm) st += (tm + '0'); reverse(all(st)); return st;}
const int N = 2e5 + 5;
// vi adj[N];
bool check(string s) {
n = s.sz;
fr(0, n - 1) {
if (s[i] == s[i + 1]) return 0;
}
return 1;
}
void solve() {
h = 0, w = 0, n = 0, m = 0, t = 0, k = 0, i = 0, j = 0,
ans = 0, num = 0, sum = 0, mo = 0, me = 0, cnt = 0, mi = inf, ma = 0;
id = 0, id1 = 0, id2 = 0;
x = 0, y = 0, z = 0; l = 0, r = 0, q = 0, a = 0, b = 0, c = 0, d = 0;
cin >> n >> s;
mii mp;
char kk = s[0], dd;
if (kk != 'R') dd = 'R';
else dd = 'Y';
ff{
if (s[i] == kk) {
mp[0]++;
}
else if (s[i] == dd) {
mp[1]++;
}
else mp[2]++;
}
a = n / 2; b = n - a;
if (mp.sz == 2) {
mp[1] = mp[2];
mp[2] = mp[1];
if ((mp[0] != a && mp[1] != b) && (mp[1] != a && mp[0] != b)) ext;
if (a == b) {
int x = 0, num = 0;
ff{
if (s[i] == s[0]) {
num += abs(x);
x--;
}
else{
x++;
}
}
mi = num;
x = 0, num = 0;
ff{
if (s[i] != s[0]) {
num += abs(x);
x--;
}
else{
x++;
}
}
mi = min(mi, num);
cout << mi;
}
else if (mp[1] > mp[0]) {
int x = 0, num = 0;
ff{
if (s[i] != s[0]) {
num += abs(x);
x--;
}
else{
x++;
}
}
cout << num;
}
else {
int x = 0, num = 0;
ff{
if (s[i] == s[0]) {
num += abs(x);
x--;
}
else{
x++;
}
}
cout << num;
}
}
else {
if (n <= 15) {
p = s;
sort(all(s)); mii mp;
do {
id = 1;
fr(0, n - 1) {
if (s[i] == s[i + 1]) {
id = 0; break;
}
}
if (id) {
num = 0;
mp.clear();
ff{
if (s[i] != p[i]) {
frj(0, n) {
if (i == j) continue;
if (s[j] == p[i] && mp[j] == 0) {
num += abs(j - i);
mp[j] = 1;
break;
}
}
}
else mp[i] = 1;
}
// cout << s << " " << num / 2; el;
mi = min(mi, (num + 1) / 2);
}
} while (next_permutation(all(s)));
cout << mi;
}
else {
}
}
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);
int T = 1;
// cin >> T;
while (T--) {
cout << fixed << setprecision(12); solve();
cout << endl;
} return 0;
}
// TULIS !!! / liat sebaliknya / semuanya / sebagian
// out of bounds / overflor / array bounds
// answer minus, edge test case / special tc
// TLE/MLE
// check other approach / solusi jangan terlalu ribet
/*
BRUTE FORCEEEEEEEE !!!!
bitmasking, sliding window, segtree, pref sum / xor / factor / prime
bipartite graph, adhoc, 2 pointer
DSU, bfs / dfs, divconquer, iterate, multiset
dp, bitmasking, binsearch, konstanta
chinese remainder theorem, __builtin_popcount, is_sorted / alpha / digit,
gcd / lcm, even / odd, min / max,multiple/divisor
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
320 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
320 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |