#include <bits/stdc++.h>
#define ll long long
#define pll pair<int, int>
#define pb push_back
#define fi first
#define se second
using namespace std;
const int MX=450;
int n, pref[MX], ans;
char c[MX];
int main(){
cin >> n;
for(int i=1; i<=n; i++){
cin >> c[i];
}
for(int i=1; i<n; i++){
// cout << i << endl;
if(c[i]!=c[i+1])continue;
int idx=i;
while(idx<=n && c[idx]==c[i]){
idx++;
}
// cout << idx << " " << i+1 <<endl;
if(idx>n) break;
ans+=idx-(i+1);
swap(c[idx], c[i+1]);
// for(int j=1; j<=n; j++) cout << c[j]; cout << endl;
}
for(int i=n; i>=2; i--){
// cout << i << endl;
if(c[i]!=c[i-1])continue;
int idx=i;
while(idx>=1 && c[idx]==c[i]){
idx--;
}
// cout << idx << " " << i-1 <<endl;
if(idx<1) break;
ans+=(i-1)-idx;
swap(c[idx], c[i-1]);
// for(int j=1; j<=n; j++) cout << c[j]; cout << endl;
}
//cek
for(int i=2; i<=n; i++){
if(c[i]==c[i-1]){
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |