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>
using namespace std;
int ans;
string s;
void swa(int i){
if(s[i]!=s[i+1]){
swap(s[i], s[i+1]);
ans++;
}
}
int main(){
int n;
cin>>n;
cin>>s;
if(n==1){
cout<<0;
return 0;
}
//s='a'+s;
for(int i=0; i<n; i++){
if(i>=2 && (s[i-1]==s[i-2] && s[i-1]!=s[i])){
for(int j=i-1; ;j--){
if(j==0 || s[j]!=s[j-1]){
ans+=i-j-1;
swap(s[i], s[j+1]);
break;
}
}
}
}
//cout<<s<<"\n";
reverse(s.begin(), s.end());
for(int i=0; i<n; i++){
if(i>=2 && (s[i-1]==s[i-2] && s[i-1]!=s[i])){
for(int j=i-1; ; j--){
if(j==0 || s[j]!=s[j-1]){
ans+=i-j-1;
swap(s[i], s[j+1]);
break;
}
}
}
}
//cout<<s<<"\n";
if(s[n-1]==s[n-2])cout<<"-1";
else cout<<ans;
}
# | 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... |