#include <bits/stdc++.h> using namespace std; using ll = long long; using Pii = pair<int,int>; using Vi = vector<int>; #define mp make_pair #define pb push_back #define x first #define y second #define rep(i,b,e) for (int i = (b); i < (e); i++) #define each(a,x) for (auto& a : (x)) #define all(x) (x).begin(),(x).end() #define sz(x) int((x).size()) #define endl '\n' const int inf = 1e9; struct Tree { int base = 1; vector<int> mini; Tree(int n) { while (base < n) base *= 2; mini.resize(2 * base, inf); } void insert(int a, int c) { a += base; mini[a] = min(mini[a], c); a /= 2; while (a >= 1) { mini[a] = min(mini[2*a], mini[2*a + 1]); a /= 2; } } int query(int a, int b) { a += base; b += base; int res = min(mini[a], mini[b]); while (a/2 != b/2) { if (a % 2 == 0) res = min(res, mini[a+1]); if (b % 2 == 1) res = min(res, mini[b-1]); a /= 2; b /= 2; } return res; } }; vector<int> getPos(vector<ll> V) { vector<int> pos(sz(V)); vector<pair<ll, int>> tmp; for (int i = 0; i < sz(V); i++) { tmp.push_back({V[i], i}); } sort(all(tmp)); for (int i = 0; i < sz(tmp); i++) { pos[tmp[i].y] = i; } return pos; } void solve() { int n; cin >> n; vector<ll> V(n+1); for (int i = 1; i <= n; i++) { cin >> V[i]; V[i] += V[i-1]; } vector<int> treePos = getPos(V); Tree tree(n+1); vector<int> dp(n+1, inf); dp[0] = 0; tree.insert(treePos[0], dp[0] - (0 + 1)); for (int i = 1; i <= n; i++) { dp[i] = i + tree.query(0, treePos[i]); tree.insert(treePos[i], dp[i] - (i + 1)); } if (dp[n] < inf) { cout << dp[n] << endl; } else { cout << -1 << endl; } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); int t = 1; //cin >> t; for (int i = 0; i < t; i++) { solve(); } }
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 | #include <bits/stdc++.h> using namespace std; using ll = long long; using Pii = pair<int,int>; using Vi = vector<int>; #define mp make_pair #define pb push_back #define x first #define y second #define rep(i,b,e) for (int i = (b); i < (e); i++) #define each(a,x) for (auto& a : (x)) #define all(x) (x).begin(),(x).end() #define sz(x) int((x).size()) #define endl '\n' const int inf = 1e9; struct Tree { int base = 1; vector<int> mini; Tree(int n) { while (base < n) base *= 2; mini.resize(2 * base, inf); } void insert(int a, int c) { a += base; mini[a] = min(mini[a], c); a /= 2; while (a >= 1) { mini[a] = min(mini[2*a], mini[2*a + 1]); a /= 2; } } int query(int a, int b) { a += base; b += base; int res = min(mini[a], mini[b]); while (a/2 != b/2) { if (a % 2 == 0) res = min(res, mini[a+1]); if (b % 2 == 1) res = min(res, mini[b-1]); a /= 2; b /= 2; } return res; } }; vector<int> getPos(vector<ll> V) { vector<int> pos(sz(V)); vector<pair<ll, int>> tmp; for (int i = 0; i < sz(V); i++) { tmp.push_back({V[i], i}); } sort(all(tmp)); for (int i = 0; i < sz(tmp); i++) { pos[tmp[i].y] = i; } return pos; } void solve() { int n; cin >> n; vector<ll> V(n+1); for (int i = 1; i <= n; i++) { cin >> V[i]; V[i] += V[i-1]; } vector<int> treePos = getPos(V); Tree tree(n+1); vector<int> dp(n+1, inf); dp[0] = 0; tree.insert(treePos[0], dp[0] - (0 + 1)); for (int i = 1; i <= n; i++) { dp[i] = i + tree.query(0, treePos[i]); tree.insert(treePos[i], dp[i] - (i + 1)); } if (dp[n] < inf) { cout << dp[n] << endl; } else { cout << -1 << endl; } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); int t = 1; //cin >> t; for (int i = 0; i < t; i++) { solve(); } } |