/***
* __ __ _ _ _
* | \/ | | | | | | |
* | \ / |_ __ | | | |_ __ | | ___ __ _____ ___ __
* | |\/| | '__| | | | | '_ \| |/ / '_ \ / _ \ \ /\ / / '_ \
* | | | | | _ | |__| | | | | <| | | | (_) \ V V /| | | |
* |_| |_|_| (_) \____/|_| |_|_|\_\_| |_|\___/ \_/\_/ |_| |_|
*
*
***/
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,a,b) for (int i=a;i<=b;i++)
#define f_(i,a,b) for (int i=a;i>=b;i--)
#define MP make_pair
#define ii pair<int,int>
#define iii pair<int,ii>
const ll mod=1000000007;
int n,dem=0;
string s;
int main() {
// freopen("","r",stdin);
// freopen("","w",stdout);
cin>>n>>s;
for (int i=1;i<n;i++) {
if (s[i]==s[i-1]) {
int pos=i;
while (s[pos]==s[pos-1]&&pos<n) pos++;
if (pos==n) break;
dem+=(pos-i);
swap(s[i],s[pos]);
// cout<<"!"<<dem<<" "<<i<<"\n";
}
}
// cout<<s<<" ";
for (int i=n-1;i>0;i--) {
if (s[i]==s[i-1]) {
int pos=i-1;
while (s[pos]==s[pos-1]&&pos>0) pos--;
if (pos==0) {
printf("-1");
return 0;
}
dem+=(i-pos);
// cout<<"!!"<<dem<<" "<<i<<"\n";
swap(s[i-1],s[pos-1]);
}
}
// cout<<s<<" ";
printf("%d", dem);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
252 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Incorrect |
2 ms |
256 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
252 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Incorrect |
2 ms |
256 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
252 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Incorrect |
2 ms |
256 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |