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>
using namespace std;
using ll = long long;
const ll big = 1e18;
int main(){
ios::sync_with_stdio(0);cin.tie(0);
ll n;
cin >> n;
vector<ll> a(n);
for(ll &i : a) cin >> i;
vector<ll> l(n,big),r(n,big);
ll cur = 0;
vector<ll> b = a;
for(ll i = 0;i<n;i++){
if (b[i]==1){
if (i+1==n) break;
if (b[i+1]==1){
b[i] = -1;
b[i+1] = -1;
cur++;
}else{
if (i+2==n||b[i+2]!=-1) break;
b[i] = -1;
b[i+2] = 1;
}
}
l[i] = cur;
}
b = a;
reverse(b.begin(),b.end());
cur = 0;
for(ll i = 0;i<n;i++){
if (b[i]==-1){
if (i+1==n) break;
if (b[i+1]==-1){
b[i] = 1;
b[i+1] = 1;
cur++;
}else{
if (i+2==n||b[i+2]!=1) break;
b[i] = 1;
b[i+2] = -1;
}
}
r[i] = cur;
}
ll ans = min(l.back(),r.back());
for(ll i = 0;i<n-1;i++){
ans = min(ans,l[i]+r[n-2-i]);
}
cout << (ans>=big?-1:ans) << "\n";
}
# | 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... |
# | 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... |