#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; id = 0;
ff{
if (s[i] == 'Y')id = 1;
}
if (id) {
ff{
if (s[i] == 'R') a++;
else b++;
}
if(abs(a-b)>1) ext;
if (a == b) {
x = 0; num = 0;
ff{
if (s[i] == 'R') {
num += abs(x);
x--;
}
else x++;
}
mi = num;
ff{
if (s[i] != 'R') {
num += abs(x);
x--;
}
else x++;
}
mi = min(mi, num);
}
else if (a > b) {
ff{
if (s[i] == 'R') {
num += abs(x);
x--;
}
else x++;
}
mi = num;
}
else {
ff{
if (s[i] != 'R') {
num += abs(x);
x--;
}
else x++;
}
mi = num;
}
cout << mi; return;
}
queue<pair<int, string>> q;
q.push({0, s});
map<string, bool> vis;
vis[s] = 1;
while (!q.empty()) {
auto [x, s] = q.front(); q.pop();
vis[s] = 1;
if (check(s)) {
cout << x; return;
}
fr(0, n - 1) {
string p = s;
swap(p[i], p[i + 1]);
if (vis[p]) continue;
q.push({x + 1, p});
}
}
ext;
}
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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1057 ms |
32584 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |