#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define sz size()
#define all(v) v.begin(),v.end()
#define fi first
#define se second
const int N = 1e5;
const int mod = 1e9+7;
const int INF = 1e9;
const int di[] = {1, -1, 0, 0};
const int dj[] = {0, 0, 1, -1};
int n;
int calc(string s,int x){
string t="";
for(int i = 1; i <= n; i++){
t+=char('0'+x);
x^=1;
}
t="!"+t;
int res=0;
for(int i = 1; i <= n; i++){
if(s[i]!=t[i]){
int pos=-1;
for(int j = i+1; j <= n; j++){
if(t[i]==s[j]){
pos=j;
break;
}
}
for(int j = pos-1; j >= i; j--){
swap(s[j],s[j+1]);
res++;
}
}
}
for(int i = 1; i <= n; i++){
if(s[i]!=t[i])
return INF;
}
return res;
}
void solve() {
string s;
cin>>n>>s;
string t="";
for(int i = 0; i < n; i++){
if(s[i]=='R')
t+="0";
else
t+="1";
}
t="!"+t;
int ans=min(calc(t,0),calc(t,1));
if(ans==INF)
ans=-1;
cout<<ans<<"\n";
}
int main() {
//freopen("cowrun.in","r",stdin);
//freopen("cowrun.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t=1;
//cin>>t;
while(t--) {
solve();
}
return 0;
}
# | 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... |