#include <bits/stdc++.h> using namespace std; typedef long long lld; typedef double lf; typedef long double llf; typedef pair<int,int> pii; typedef pair<lld,lld> pll; #define For(i,s,a) for(int i = (int)s; i < (int)a; ++i) #define rpt(s, it) for(auto it = s.begin(); it != s.end(); ++it) #define brpt(s, it) for(auto it = s.rend(); it != s.rbegin(); --it) #define sz size() #define pb push_back #define eb emplace_back #define ff first #define dd second #define mp make_pair #define all(x) (x).begin (x).end() #define ZAPS {int t; scanf("%i", &t); while(t--) solve();} template<typename Ta, typename Tb> ostream & operator <<(ostream & os, pair<Ta, Tb> x){ return os << x.ff << " " << x.dd; } const int N = 5e5 + 1; const int inf = 0x3f3f3f3f; lld power[N]; lld sum[N]; int dp[2][N]; const int H = 18e6; const lld ran = (1ll << 50) - 1; struct Tre{ int nodes[H], left[H], right[H]; int offset; int size; Tre(){ } void clear(){ memset(nodes, inf, sizeof(nodes)); memset(left, 0, sizeof(left)); memset(right, 0, sizeof(right)); size = 1; offset = 0; } void push_all(){ ++nodes[1]; } int get(lld a, lld b, int w = 1, lld L = -ran, lld R = ran){ if(a <= L && R <= b){ //cout<<offset<<" "<<nodes[w]<<" "<<a<<" "<<b<<" "<<L<<" "<<R<<endl; return offset + nodes[w];} if(L > b || R < a) return inf; lld mid = (L + R) >> 1ll; int wyn = inf; if(left[w]) wyn = min(wyn, get(a, b, left[w], L, mid)); if(right[w]) wyn = min(wyn, get(a, b, right[w], mid + 1, R)); return wyn; } void upd(lld pos, int score, int w = 1, lld L = -ran, lld R = ran){ nodes[w] = min(nodes[w], score); if(L == R) return; lld mid = (L + R) >> 1ll; if(pos <= mid){ if(!left[w]) left[w] = ++size; upd(pos, score, left[w], L, mid); } else{ if(!right[w]) right[w] = ++size; upd(pos, score, right[w], mid + 1, R); } } void do_upd(lld pos, int score){ ++offset; score -= offset; this -> upd(pos, score); } }; Tre tre; lld pre(int a, int b){ return sum[b] - (a ? sum[a - 1] : 0); } bool valid(int a, int b){ return pre(a, b) >= 0; } void solve(int rot, int n){ tre.clear(); sum[0] = power[0]; For(i, 1, n) sum[i] = sum[i - 1] + power[i]; For(i, 0, n){ dp[rot][i] = inf; if(valid(0, i)){ //cout<<i<<" "<<rot<<" "<<sum[i]<<" "<<tre.get(-ran, sum[i])<<endl; dp[rot][i] = min(i, tre.get(-ran, sum[i])); } tre.do_upd(sum[i], dp[rot][i]); //cout<<rot<<" "<<i<<" "<<dp[rot][i]<<endl; // cout<<dp[rot][i]<<" "; } //puts(""); } int32_t main(void){ int n; scanf("%d", &n); For(i, 0, n) scanf("%lld", &power[i]); solve(0, n); reverse(power, power + n); solve(1, n); reverse(dp[1], dp[1] + n); int wyn = inf; For(i, -1, n){ wyn = min(wyn, (i > -1 ? dp[0][i] : 0) + (i + 1 < n ? dp[1][i + 1] : 0)); } printf("%d\n", wyn == inf ? -1 : wyn); } /* 17 2 -5 0 2 0 0 0 4 0 0 -1 4 0 0 0 0 -3 */
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | #include <bits/stdc++.h> using namespace std; typedef long long lld; typedef double lf; typedef long double llf; typedef pair<int,int> pii; typedef pair<lld,lld> pll; #define For(i,s,a) for(int i = (int)s; i < (int)a; ++i) #define rpt(s, it) for(auto it = s.begin(); it != s.end(); ++it) #define brpt(s, it) for(auto it = s.rend(); it != s.rbegin(); --it) #define sz size() #define pb push_back #define eb emplace_back #define ff first #define dd second #define mp make_pair #define all(x) (x).begin (x).end() #define ZAPS {int t; scanf("%i", &t); while(t--) solve();} template<typename Ta, typename Tb> ostream & operator <<(ostream & os, pair<Ta, Tb> x){ return os << x.ff << " " << x.dd; } const int N = 5e5 + 1; const int inf = 0x3f3f3f3f; lld power[N]; lld sum[N]; int dp[2][N]; const int H = 18e6; const lld ran = (1ll << 50) - 1; struct Tre{ int nodes[H], left[H], right[H]; int offset; int size; Tre(){ } void clear(){ memset(nodes, inf, sizeof(nodes)); memset(left, 0, sizeof(left)); memset(right, 0, sizeof(right)); size = 1; offset = 0; } void push_all(){ ++nodes[1]; } int get(lld a, lld b, int w = 1, lld L = -ran, lld R = ran){ if(a <= L && R <= b){ //cout<<offset<<" "<<nodes[w]<<" "<<a<<" "<<b<<" "<<L<<" "<<R<<endl; return offset + nodes[w];} if(L > b || R < a) return inf; lld mid = (L + R) >> 1ll; int wyn = inf; if(left[w]) wyn = min(wyn, get(a, b, left[w], L, mid)); if(right[w]) wyn = min(wyn, get(a, b, right[w], mid + 1, R)); return wyn; } void upd(lld pos, int score, int w = 1, lld L = -ran, lld R = ran){ nodes[w] = min(nodes[w], score); if(L == R) return; lld mid = (L + R) >> 1ll; if(pos <= mid){ if(!left[w]) left[w] = ++size; upd(pos, score, left[w], L, mid); } else{ if(!right[w]) right[w] = ++size; upd(pos, score, right[w], mid + 1, R); } } void do_upd(lld pos, int score){ ++offset; score -= offset; this -> upd(pos, score); } }; Tre tre; lld pre(int a, int b){ return sum[b] - (a ? sum[a - 1] : 0); } bool valid(int a, int b){ return pre(a, b) >= 0; } void solve(int rot, int n){ tre.clear(); sum[0] = power[0]; For(i, 1, n) sum[i] = sum[i - 1] + power[i]; For(i, 0, n){ dp[rot][i] = inf; if(valid(0, i)){ //cout<<i<<" "<<rot<<" "<<sum[i]<<" "<<tre.get(-ran, sum[i])<<endl; dp[rot][i] = min(i, tre.get(-ran, sum[i])); } tre.do_upd(sum[i], dp[rot][i]); //cout<<rot<<" "<<i<<" "<<dp[rot][i]<<endl; // cout<<dp[rot][i]<<" "; } //puts(""); } int32_t main(void){ int n; scanf("%d", &n); For(i, 0, n) scanf("%lld", &power[i]); solve(0, n); reverse(power, power + n); solve(1, n); reverse(dp[1], dp[1] + n); int wyn = inf; For(i, -1, n){ wyn = min(wyn, (i > -1 ? dp[0][i] : 0) + (i + 1 < n ? dp[1][i + 1] : 0)); } printf("%d\n", wyn == inf ? -1 : wyn); } /* 17 2 -5 0 2 0 0 0 4 0 0 -1 4 0 0 0 0 -3 */ |