#include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define PR std::pair #define MP std::make_pair #define X first #define Y second typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; const ll INF = 1000000666*1000000000ll; int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); int n; std::cin >> n; VI A(n); FOR(i, n) std::cin >> A[i]; std::set<PR<ll, int> > set; set.insert(MP(0ll, n)); ll pref = 0; int ans = -1; FOR(i, n){ ll a = A[i]; pref += a; auto it = set.upper_bound(MP(pref, 1000000000)); if(it == set.begin()) continue; it--; int my = it->Y - (n-i); if(i == n-1){ ans = my; } it = set.insert(MP(pref, my + (n-1-i))).X; if(it != set.begin() && std::prev(it)->Y <= it->Y){ set.erase(it); continue; } while(true){ auto nxt = std::next(it); if(nxt == set.end()) break; if(nxt->Y >= it->Y) set.erase(nxt); else break; } } std::cout << ans << "\n"; return 0; }
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 | #include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define PR std::pair #define MP std::make_pair #define X first #define Y second typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; const ll INF = 1000000666*1000000000ll; int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); int n; std::cin >> n; VI A(n); FOR(i, n) std::cin >> A[i]; std::set<PR<ll, int> > set; set.insert(MP(0ll, n)); ll pref = 0; int ans = -1; FOR(i, n){ ll a = A[i]; pref += a; auto it = set.upper_bound(MP(pref, 1000000000)); if(it == set.begin()) continue; it--; int my = it->Y - (n-i); if(i == n-1){ ans = my; } it = set.insert(MP(pref, my + (n-1-i))).X; if(it != set.begin() && std::prev(it)->Y <= it->Y){ set.erase(it); continue; } while(true){ auto nxt = std::next(it); if(nxt == set.end()) break; if(nxt->Y >= it->Y) set.erase(nxt); else break; } } std::cout << ans << "\n"; return 0; } |