#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], res;
vector<char>cc(MX), tmp;
int check1(vector<char>c){
int ans=0;
for(int i=1; i<n; i++){
if(c[i]!=c[i+1])continue;
int idx=i;
while(idx<=n && c[idx]==c[i]){
idx++;
}
if(idx>n) break;
ans+=idx-(i+1);
swap(c[idx], c[i+1]);
}
for(int i=n; i>=2; i--){
if(c[i]!=c[i-1])continue;
int idx=i;
while(idx>=1 && c[idx]==c[i]){
idx--;
}
if(idx<1) break;
ans+=(i-1)-idx;
swap(c[idx], c[i-1]);
}
for(int i=2; i<=n; i++){
if(c[i]==c[i-1]){
// cout << -1 << endl;
return -1;
}
}
return ans;
}
int check2(vector<char>c){
int ans=0;
for(int i=n; i>=2; i--){
if(c[i]!=c[i-1])continue;
int idx=i;
while(idx>=1 && c[idx]==c[i]){
idx--;
}
if(idx<1) break;
ans+=(i-1)-idx;
swap(c[idx], c[i-1]);
}
for(int i=1; i<n; i++){
if(c[i]!=c[i+1])continue;
int idx=i;
while(idx<=n && c[idx]==c[i]){
idx++;
}
if(idx>n) break;
ans+=idx-(i+1);
swap(c[idx], c[i+1]);
}
for(int i=2; i<=n; i++){
if(c[i]==c[i-1]){
// cout << -1 << endl;
return -1;
}
}
return ans;
}
int main(){
cin >> n;
for(int i=1; i<=n; i++){
cin >> cc[i];
}
tmp=cc;
int ans=1e9;
// cout << check1(cc) << " " << check2(cc)<< endl;
res=min(check1(cc), check2(cc));
cout << res << endl;
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:80:6: warning: unused variable 'ans' [-Wunused-variable]
80 | int ans=1e9;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |