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>
#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 (stderr)
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:80:6: warning: unused variable 'ans' [-Wunused-variable]
80 | int ans=1e9;
| ^~~
# | 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... |