#include <bits/stdc++.h> using namespace std; const int N = 500100; int n; long long a[N], pref[N]; int t[N*4], sz; void build(int v, int l, int r) { t[v] = 1e9; if (l == r) return; int mid = (l+r)>>1; build(v+v, l, mid); build(v+v+1, mid+1, r); } int get(int v, int l, int r, int tl, int tr) { if (l > r || tl > r || l > tr || tl > tr) return 1e9; if (tl <= l && r <= tr) return t[v]; int mid = (l+r)>>1; return min(get(v+v, l, mid, tl, tr), get(v+v+1, mid+1, r, tl, tr)); } void update(int v, int l, int r, int pos, int x) { if (l == r) { t[v] = min(t[v], x); return; } int mid = (l+r)>>1; if (pos <= mid) update(v+v, l, mid, pos, x); else update(v+v+1, mid+1, r, pos, x); t[v] = min(t[v+v], t[v+v+1]); } int nxtpos[N]; void solve() { cin >> n; vector<int> vecc; for (int i = 1; i <= n; i++) { cin >> a[i]; pref[i] = pref[i-1] + a[i]; vecc.push_back(pref[i]); } if (pref[n] < 0) { cout << -1 << "\n"; return; } nxtpos[n+1] = n+1; for (int i = n; i >= 1; i--){ nxtpos[i] = nxtpos[i+1]; if (a[i] < 0) nxtpos[i] = i; } vecc.push_back(0); sort(vecc.begin(), vecc.end()); vecc.erase(unique(vecc.begin(), vecc.end())); sz = vecc.size(); build(1, 1, sz); int dprst = 0; int pos_zero = lower_bound(vecc.begin(), vecc.end(), 0)-vecc.begin()+1; update(1, 1, sz, pos_zero, 0); for (int i = 1; i <= n; i++) { int pos = lower_bound(vecc.begin(), vecc.end(), pref[i])-vecc.begin()+1; if (a[i] < 0) dprst = 1e9; int mn = get(1, 1, sz, 1, pos)+i-1; dprst = min(dprst, mn); update(1, 1, sz, pos, dprst-i); } cout << dprst << "\n"; } int main() { ios_base::sync_with_stdio(0);cin.tie(0); int tt = 1; while(tt--) { solve(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | #include <bits/stdc++.h> using namespace std; const int N = 500100; int n; long long a[N], pref[N]; int t[N*4], sz; void build(int v, int l, int r) { t[v] = 1e9; if (l == r) return; int mid = (l+r)>>1; build(v+v, l, mid); build(v+v+1, mid+1, r); } int get(int v, int l, int r, int tl, int tr) { if (l > r || tl > r || l > tr || tl > tr) return 1e9; if (tl <= l && r <= tr) return t[v]; int mid = (l+r)>>1; return min(get(v+v, l, mid, tl, tr), get(v+v+1, mid+1, r, tl, tr)); } void update(int v, int l, int r, int pos, int x) { if (l == r) { t[v] = min(t[v], x); return; } int mid = (l+r)>>1; if (pos <= mid) update(v+v, l, mid, pos, x); else update(v+v+1, mid+1, r, pos, x); t[v] = min(t[v+v], t[v+v+1]); } int nxtpos[N]; void solve() { cin >> n; vector<int> vecc; for (int i = 1; i <= n; i++) { cin >> a[i]; pref[i] = pref[i-1] + a[i]; vecc.push_back(pref[i]); } if (pref[n] < 0) { cout << -1 << "\n"; return; } nxtpos[n+1] = n+1; for (int i = n; i >= 1; i--){ nxtpos[i] = nxtpos[i+1]; if (a[i] < 0) nxtpos[i] = i; } vecc.push_back(0); sort(vecc.begin(), vecc.end()); vecc.erase(unique(vecc.begin(), vecc.end())); sz = vecc.size(); build(1, 1, sz); int dprst = 0; int pos_zero = lower_bound(vecc.begin(), vecc.end(), 0)-vecc.begin()+1; update(1, 1, sz, pos_zero, 0); for (int i = 1; i <= n; i++) { int pos = lower_bound(vecc.begin(), vecc.end(), pref[i])-vecc.begin()+1; if (a[i] < 0) dprst = 1e9; int mn = get(1, 1, sz, 1, pos)+i-1; dprst = min(dprst, mn); update(1, 1, sz, pos, dprst-i); } cout << dprst << "\n"; } int main() { ios_base::sync_with_stdio(0);cin.tie(0); int tt = 1; while(tt--) { solve(); } } |