#include "bits/stdc++.h" using namespace std; template <class A, class B> ostream& operator<<(ostream &os, pair<A, B> p) { return os << '{' << p.first << ", " << p.second << '}'; } template <class T> ostream& operator<<(ostream &os, vector<T> v) { os << '{'; for (T i : v) os << i << ", "; return os << '}'; } #ifdef DEBUG #define D(x...) x #else #define D(x...) #endif #define LN(x) D(cerr << #x << ": " << x << ' ') #define LOG(x) D(cerr << #x << ": " << x << '\n') #define MES(x) D(cerr << x << ' ') #define MESL(x) D(cerr << x << '\n') typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; #define EB emplace_back #define PB pop_back #define fi first #define se second #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) // Ignacy Boehlke int main() { int n; scanf("%d", &n); int in, dis = 0, len = 0; ll sum = 0; set<pair<ll, int>> S; S.emplace(0, 0); auto it = S.begin(); REP(i, n) { scanf("%d", &in); ++len; if (in == 0) continue; it = S.upper_bound({sum, INT_MAX}); if (it != S.begin()) { --it; pair<ll, int> val = {sum, it->se + len}; it = S.lower_bound({val.fi, INT_MAX}); if (it == S.begin() || (--it)->se < val.se) { if (it->fi == val.fi) it = S.erase(it); else ++it; while (it != S.end() && it->se <= val.se) it = S.erase(it); S.emplace(val); } } sum += in; dis += len; len = 0; D(for (auto s : S) fprintf(stderr, "{%lld, %d}, ", s.fi, s.se); fprintf(stderr, "\n");) } it = S.upper_bound({sum, INT_MAX}); if (it == S.begin()) puts("-1"); else printf("%d\n", dis - (--it)->se); }
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 | #include "bits/stdc++.h" using namespace std; template <class A, class B> ostream& operator<<(ostream &os, pair<A, B> p) { return os << '{' << p.first << ", " << p.second << '}'; } template <class T> ostream& operator<<(ostream &os, vector<T> v) { os << '{'; for (T i : v) os << i << ", "; return os << '}'; } #ifdef DEBUG #define D(x...) x #else #define D(x...) #endif #define LN(x) D(cerr << #x << ": " << x << ' ') #define LOG(x) D(cerr << #x << ": " << x << '\n') #define MES(x) D(cerr << x << ' ') #define MESL(x) D(cerr << x << '\n') typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; #define EB emplace_back #define PB pop_back #define fi first #define se second #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) // Ignacy Boehlke int main() { int n; scanf("%d", &n); int in, dis = 0, len = 0; ll sum = 0; set<pair<ll, int>> S; S.emplace(0, 0); auto it = S.begin(); REP(i, n) { scanf("%d", &in); ++len; if (in == 0) continue; it = S.upper_bound({sum, INT_MAX}); if (it != S.begin()) { --it; pair<ll, int> val = {sum, it->se + len}; it = S.lower_bound({val.fi, INT_MAX}); if (it == S.begin() || (--it)->se < val.se) { if (it->fi == val.fi) it = S.erase(it); else ++it; while (it != S.end() && it->se <= val.se) it = S.erase(it); S.emplace(val); } } sum += in; dis += len; len = 0; D(for (auto s : S) fprintf(stderr, "{%lld, %d}, ", s.fi, s.se); fprintf(stderr, "\n");) } it = S.upper_bound({sum, INT_MAX}); if (it == S.begin()) puts("-1"); else printf("%d\n", dis - (--it)->se); } |