#include <bits/stdc++.h>
using namespace std;
#define print(l) for(auto i:l) cout<<i<<" ";cout<<endl;
#define input(t,l,n) vector<t>l(n);for(int i = 0;i<n;i++)cin>>l[i];
#define int long long
#define pb push_back
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define all(l) l.begin(),l.end()
#define pii pair<int,int>
#define fi first
#define se second
const int M = 1e9+7;
const int inf = 1e18;
int bp(int x, int y, int p){
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int MI(int n, int p){
return bp(n, p - 2, p);
}
int mul(int x,int y, int p){
return x * 1ull * y % p;
}
int di(int x,int y, int p){
return mul(x, MI(y, p), p);
}
int n , m , k , q;
void solve(int testcase_number){
cin>>n;
input(int,l,n);
while(l.back() == 1){
l.pop_back();
}
reverse(all(l));
while(l.back() == -1){
l.pop_back();
}
reverse(all(l));
n = l.size();
// print(l);
vector<vector<int>>comps;
int f = l[0];
int i = 0;
vector<int>assi(n+1,-1);
vector<int>t;
while(i<n){
while(l[i] == f){
t.pb(l[i]);
assi[i] = comps.size();
i++;
}
f = l[i];
comps.pb(t);
t = {};
}
n = comps.size();
// for(auto i:comps){
// cout<<i[0]<<" ";
// }
// cout<<endl;
// for(auto i:comps){
// cout<<i.size()<<" ";
// }
// cout<<endl;
vector<int>pre(n+1,0);
vector<int>suff(n+1,0);
for(int i = 0;i<n;i++){
if(comps[i].size()%2 == 1 and comps[i][0] == 1){
pre[i] = 1;
}
if(i-1>=0){
pre[i]|=pre[i-1];
}
}
for(int i = n-1;i>=0;i--){
if(comps[i].size()%2 == 1 and comps[i][0] == -1){
suff[i] = 1;
}
if(i+1<n){
suff[i]|=suff[i+1];
}
}
int sum = 0;
for(auto i:comps){
if(i[0] == -1){
if(i.size()%2 == 0) sum+=i.size()/2;
}
}
int ans = 1e18;
int sum2 =0 ;
for(int i = 0;i<n;i++){
// cout<<sum2<<" "<<sum<<endl;
if(suff[i] == 0 and comps[i][0] == -1){
sum-=comps[i].size()/2;
}
if(pre[i] == 0 and suff[i] == 0){
ans = min(ans,sum+sum2);
// cout<<ans<<" ";
}
if(pre[i] == 0 and comps[i][0] == 1){
sum2+=comps[i].size()/2;
}
}
// print(pre);
// print(suff);
if(ans == 1e18){
cout<<-1<<endl;
return;
}
cout<<ans<<endl;
// for(int i = 0;i<n;i++){
//
// }
// int ne = 1;
// int po = 1;
// int toup = 1;
// int ans1 = 0;
// int fne = 0;
// int ans2 = 0;
// for(auto i:comps){
// // print(i);
// if(i[0] == -1){
// if(i.size()%2 !=0){
// ne = 0;
// }
// else{
// ans1+=(i.size()/2);
// }
// }
// else{
//
// if(i.size()%2!=0){
// // toup = 0;
// po =0 ;
// }
// else{
// ans2+=(i.size()/2);
// }
// }
// }
// // cout<<ne<<" "<<po<<endl;
// if(ne == 0 and po == 0){
// cout<<-1<<endl;
// }
// else if(ne == 0 and po == 1){
// cout<<ans2<<endl;
// }
// else if(ne == 1 and po == 0){
// cout<<ans1<<endl;
// }
// else{
// cout<<min(ans1,ans2)<<endl;
// }
}
signed main(){
ios::sync_with_stdio(0);//DO NOT USE IN INTERACTIVE
cin.tie(0), cout.tie(0);
cout << fixed<<setprecision(9);
int t = 1;
// cin>>t;
for(int i = 1;i<=t;i++){
solve(i);
}
}
| # | 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... |