#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second typedef pair<ll,int> pli; const int inf = 1000100; int n; int a[500500]; ll glob_x; int glob_y; set<pli> s; void insert(int x, int y) { ll xr = -glob_x + x; int yr = y - glob_y; pli p(xr, -inf); auto it = s.lower_bound(p); while (it != s.begin()) { it--; if (it ->se >= yr) { auto it2=it; it++; s.erase(it2); } else { it++; break; } } p.se = yr; if (it == s.end()) { s.insert(p); return; } if (it -> fi == xr) { if (it -> se > yr) { s.erase(it); s.insert(p); } return; } if (it -> se > yr) { s.insert(p); } } void add(int p) { glob_x += p; } void incr() { glob_y++; } int get() { ll x0 = -glob_x; pli p(x0, -inf); auto it = s.lower_bound(p); if (it == s.end()) return inf; return it -> se + glob_y; } int main() { scanf("%d", &n); FORI(i,n) scanf("%d", &a[i]); insert(0,0); FORI(i,n) { int y = get(); add(a[i]); incr(); if (y<inf) { insert(a[i], y); } //for (auto it : s) { printf("(%lld, %d) ", it.fi+glob_x, it.se+glob_y); } //printf("\n"); } int y = get(); printf("%d\n", y==inf?-1:y); 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 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 | #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second typedef pair<ll,int> pli; const int inf = 1000100; int n; int a[500500]; ll glob_x; int glob_y; set<pli> s; void insert(int x, int y) { ll xr = -glob_x + x; int yr = y - glob_y; pli p(xr, -inf); auto it = s.lower_bound(p); while (it != s.begin()) { it--; if (it ->se >= yr) { auto it2=it; it++; s.erase(it2); } else { it++; break; } } p.se = yr; if (it == s.end()) { s.insert(p); return; } if (it -> fi == xr) { if (it -> se > yr) { s.erase(it); s.insert(p); } return; } if (it -> se > yr) { s.insert(p); } } void add(int p) { glob_x += p; } void incr() { glob_y++; } int get() { ll x0 = -glob_x; pli p(x0, -inf); auto it = s.lower_bound(p); if (it == s.end()) return inf; return it -> se + glob_y; } int main() { scanf("%d", &n); FORI(i,n) scanf("%d", &a[i]); insert(0,0); FORI(i,n) { int y = get(); add(a[i]); incr(); if (y<inf) { insert(a[i], y); } //for (auto it : s) { printf("(%lld, %d) ", it.fi+glob_x, it.se+glob_y); } //printf("\n"); } int y = get(); printf("%d\n", y==inf?-1:y); return 0; } |