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;
typedef long long ll;
typedef pair<int, int> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#pragma GCC optimize("Ofast")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define int long long
#define lowbit(x) x&-x
const int maxn = 1e6 + 5;
const int N = 1e6 + 3;
const int INF = 1e18;
int a[maxn], dp[maxn][2];
signed main(void){
fastio;
int n;
cin>>n;
for(int i = 0; i < n; i++) cin>>a[i];
int ans = INF;
for(int i = 0; i < n; i++){
if(a[i] == 1){
if(i && a[i - 1] == 1) dp[i + 1][0] = dp[i - 1][0] + 1;
else dp[i + 1][0] = INF;
}
else{
dp[i + 1][0] = dp[i][0];
}
}
for(int i = n - 1; i >= 0; i--){
if(a[i] == -1){
if(i < n - 1 && a[i + 1] == -1) dp[i][1] = dp[i + 2][1] + 1;
else dp[i][1] = INF;
}
else{
dp[i][1] = dp[i + 1][1];
}
}
for(int i = 0; i <= n; i++) ans = min(ans, dp[i][0] + dp[i][1]);
cout<<(ans < INF ? ans : -1)<<"\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... |