#include <iostream> #include <set> #include <vector> #include <utility> #include <map> #include <queue> #include <algorithm> #include <string> #include <map> using namespace std; #define PB push_back #define MP make_pair #define F first #define S second typedef long long ll; typedef pair<int, int> pii; const int INF = 1e9; int n, P = 1; vector<ll> v, soso; vector<pair<int, pair<ll, int>>> positive; set<int> ms; // pozycja, suma, liczba vector<int> tree; void buildTree(int x) { while (P < x) { P *= 2; } tree.resize(2 * P, 0); } int query(int a, int b) { if (a > b) { return 0; } a += P - 1; b += P - 1; int res = max(tree[a], tree[b]); while (a / 2 != b / 2) { if (a % 2 == 0) res = max(res, tree[a + 1]); if (b % 2 == 1) res = max(res, tree[b - 1]); a /= 2; b /= 2; } return res; } void insert(int pos, int res) { pos += P - 1; tree[pos] = max(tree[pos], res); pos /= 2; while (pos) { tree[pos] = max(tree[2 * pos], tree[2 * pos + 1]); pos /= 2; } } void solve() { cin >> n; v.resize(n + 1, 0); for (int i = 0; i < n; i++) { cin >> v[i + 1]; } int zero = 0; bool addZero = true; ll sum = 0; for (int i = 1; i <= n; i++) { if (v[i]) { addZero = false; sum += v[i]; if (sum >= 0) { positive.PB(MP(i, MP(sum, 1))); soso.PB(sum); addZero = true; } zero = 0; } else { zero++; if (addZero && positive.size()) { positive[positive.size() - 1].S.S++; } } } if (positive.size() == 0) { cout << -1 << endl; exit(0); } positive[positive.size() - 1].S.S = 0; sort(soso.begin(), soso.end()); soso.erase(unique(soso.begin(), soso.end()), soso.end()); vector<pii> good; vector<ll>::iterator it; for (pair<int, pii> p : positive) { it = lower_bound(soso.begin(), soso.end(), p.S.F); ll dist = (it - soso.begin()); dist++; good.PB(MP(dist, p.S.S)); } buildTree(good.size()); vector<int> res(good.size(), 0); for (int i = 0; i < good.size(); i++) { pii p = good[i]; int q = query(1, p.F); res[i] = q + p.S; insert(p.F, res[i]); } cout << n - 1 - res[good.size() - 1] - zero << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); solve(); 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 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 | #include <iostream> #include <set> #include <vector> #include <utility> #include <map> #include <queue> #include <algorithm> #include <string> #include <map> using namespace std; #define PB push_back #define MP make_pair #define F first #define S second typedef long long ll; typedef pair<int, int> pii; const int INF = 1e9; int n, P = 1; vector<ll> v, soso; vector<pair<int, pair<ll, int>>> positive; set<int> ms; // pozycja, suma, liczba vector<int> tree; void buildTree(int x) { while (P < x) { P *= 2; } tree.resize(2 * P, 0); } int query(int a, int b) { if (a > b) { return 0; } a += P - 1; b += P - 1; int res = max(tree[a], tree[b]); while (a / 2 != b / 2) { if (a % 2 == 0) res = max(res, tree[a + 1]); if (b % 2 == 1) res = max(res, tree[b - 1]); a /= 2; b /= 2; } return res; } void insert(int pos, int res) { pos += P - 1; tree[pos] = max(tree[pos], res); pos /= 2; while (pos) { tree[pos] = max(tree[2 * pos], tree[2 * pos + 1]); pos /= 2; } } void solve() { cin >> n; v.resize(n + 1, 0); for (int i = 0; i < n; i++) { cin >> v[i + 1]; } int zero = 0; bool addZero = true; ll sum = 0; for (int i = 1; i <= n; i++) { if (v[i]) { addZero = false; sum += v[i]; if (sum >= 0) { positive.PB(MP(i, MP(sum, 1))); soso.PB(sum); addZero = true; } zero = 0; } else { zero++; if (addZero && positive.size()) { positive[positive.size() - 1].S.S++; } } } if (positive.size() == 0) { cout << -1 << endl; exit(0); } positive[positive.size() - 1].S.S = 0; sort(soso.begin(), soso.end()); soso.erase(unique(soso.begin(), soso.end()), soso.end()); vector<pii> good; vector<ll>::iterator it; for (pair<int, pii> p : positive) { it = lower_bound(soso.begin(), soso.end(), p.S.F); ll dist = (it - soso.begin()); dist++; good.PB(MP(dist, p.S.S)); } buildTree(good.size()); vector<int> res(good.size(), 0); for (int i = 0; i < good.size(); i++) { pii p = good[i]; int q = query(1, p.F); res[i] = q + p.S; insert(p.F, res[i]); } cout << n - 1 - res[good.size() - 1] - zero << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); solve(); return 0; } |