This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
//qwerty47924692
using namespace std;
using ll = long long;
#define f first
#define s second
const ll N=3e5+29;
ll cur1,cur2,cnt[2],n;
string s;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>s;
for(char i:s){
if(i=='G')cnt[0]++;
else cnt[1]++;
}
ll ans=1e18;
if(abs(cnt[1]-cnt[0])>1){
cout<<-1;
return 0;
}
if(cnt[1]>=cnt[0]){
cur1=0;
cur2=1;
ll sum=0;
for(ll i=0;i<n;i++){
if(s[i]=='R'){
sum+=abs(i-cur1);
cur1+=2;
}
else{
sum+=abs(i-cur2);
cur2+=2;
}
}
ans=min(ans,sum);
}
if(cnt[1]<=cnt[0]){
cur1=1;
cur2=0;
ll sum=0;
for(ll i=0;i<n;i++){
if(s[i]=='R'){
sum+=abs(i-cur1);
cout<<cur1<<' ';
cur1+=2;
}
else{
sum+=abs(i-cur2);
cout<<cur2<<' ';
cur2+=2;
}
}
ans=min(ans,sum);
}
cout<<ans/2;
}
# | 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... |