#include <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; typedef long long ll; int BLOK; const int INF = 1000000000; struct ele { ll suf; int b, nr; }; bool por(ele a, ele b) { if (a.suf < b.suf) return true; if (a.suf > b.suf) return false; return a.nr < b.nr; } ll Pref(vector<ll>& a, int l, int r) { ll s = a[r]; if (l != 0) s -= a[l - 1]; return s; } int Szukaj(vector<ele>& a, ll x) { int l = 0, p = a.size() - 1, s, r = -1; while (l <= p) { s = (l + p) >> 1; if (a[s].suf >= x) { r = s; p = s - 1; } else l = s + 1; } return r; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; BLOK = 1000; vector<ll> pref(n); for (int i = 0; i < n; ++i) { cin >> pref[i]; if (i != 0) pref[i] += pref[i - 1]; } if (pref[n - 1] < 0) { cout << "-1"; return 0; } vector<int> l, p; for (int i = 0; i < n; i += BLOK) { l.push_back(i); p.push_back(min(i + BLOK - 1, n - 1)); } int ilBlokow = l.size(); vector<int> dp(n, INF); vector<ll> sumBloku(ilBlokow, 0); vector< vector<ele> > wart(ilBlokow); for (int i = 0; i < ilBlokow; ++i) { int L = l[i], P = p[i]; for (int j = L; j <= P; ++j) { if (j != 0 && pref[j] == pref[j - 1]) { dp[j] = dp[j - 1]; continue; } for (int k = L; k <= j; ++k) { if (k != 0) { if (Pref(pref, k, j) >= 0 && dp[k - 1] != INF) dp[j] = min(dp[j], dp[k - 1] + j - k); } else { if (Pref(pref, k, j) >= 0) dp[j] = min(dp[j], j); } } for (int k = 0; k < i; ++k) { sumBloku[k] += Pref(pref, j, j); int gdzie = Szukaj(wart[k], -sumBloku[k]); if (gdzie != -1) dp[j] = min(dp[j], j + wart[k][gdzie].b); } } for (int j = L; j <= P; ++j) { if (j == 0) wart[i].push_back({Pref(pref, j, P), 0, j}); else { if (dp[j - 1] != INF) wart[i].push_back({Pref(pref, j, P), dp[j - 1] - j, j}); } } sort(wart[i].begin(), wart[i].end(), por); for (int j = (int) wart[i].size() - 2; j >= 0; --j) wart[i][j].b = min(wart[i][j].b, wart[i][j + 1].b); } cout << dp[n - 1]; 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 | #include <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; typedef long long ll; int BLOK; const int INF = 1000000000; struct ele { ll suf; int b, nr; }; bool por(ele a, ele b) { if (a.suf < b.suf) return true; if (a.suf > b.suf) return false; return a.nr < b.nr; } ll Pref(vector<ll>& a, int l, int r) { ll s = a[r]; if (l != 0) s -= a[l - 1]; return s; } int Szukaj(vector<ele>& a, ll x) { int l = 0, p = a.size() - 1, s, r = -1; while (l <= p) { s = (l + p) >> 1; if (a[s].suf >= x) { r = s; p = s - 1; } else l = s + 1; } return r; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; BLOK = 1000; vector<ll> pref(n); for (int i = 0; i < n; ++i) { cin >> pref[i]; if (i != 0) pref[i] += pref[i - 1]; } if (pref[n - 1] < 0) { cout << "-1"; return 0; } vector<int> l, p; for (int i = 0; i < n; i += BLOK) { l.push_back(i); p.push_back(min(i + BLOK - 1, n - 1)); } int ilBlokow = l.size(); vector<int> dp(n, INF); vector<ll> sumBloku(ilBlokow, 0); vector< vector<ele> > wart(ilBlokow); for (int i = 0; i < ilBlokow; ++i) { int L = l[i], P = p[i]; for (int j = L; j <= P; ++j) { if (j != 0 && pref[j] == pref[j - 1]) { dp[j] = dp[j - 1]; continue; } for (int k = L; k <= j; ++k) { if (k != 0) { if (Pref(pref, k, j) >= 0 && dp[k - 1] != INF) dp[j] = min(dp[j], dp[k - 1] + j - k); } else { if (Pref(pref, k, j) >= 0) dp[j] = min(dp[j], j); } } for (int k = 0; k < i; ++k) { sumBloku[k] += Pref(pref, j, j); int gdzie = Szukaj(wart[k], -sumBloku[k]); if (gdzie != -1) dp[j] = min(dp[j], j + wart[k][gdzie].b); } } for (int j = L; j <= P; ++j) { if (j == 0) wart[i].push_back({Pref(pref, j, P), 0, j}); else { if (dp[j - 1] != INF) wart[i].push_back({Pref(pref, j, P), dp[j - 1] - j, j}); } } sort(wart[i].begin(), wart[i].end(), por); for (int j = (int) wart[i].size() - 2; j >= 0; --j) wart[i][j].b = min(wart[i][j].b, wart[i][j + 1].b); } cout << dp[n - 1]; return 0; } |