#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 |
1 |
Correct |
3 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 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 |
3 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 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 |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
212 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 |
3 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |