#include <cstdio> #include <cstdint> #include <vector> using namespace std; typedef pair<int64_t, int> city_t; int bsearch(vector<city_t>& v, int l, int r, city_t key) { while (r - l > 1) { int m = l + (r - l) / 2; if (v[m] >= key) { r = m; } else { l = m; } } return r; } int lis(vector<city_t>& v) { if (v.size() == 0) { return 0; } vector<city_t> tail(v.size(), make_pair(-10e8, 0)); int length = 1; tail[0] = v[0]; for (size_t i = 1; i < v.size(); i++) { if (v[i] < tail[0]) { tail[0] = v[i]; } else if (v[i] > tail[length - 1]) { tail[length++] = v[i]; } else { tail[bsearch(tail, -1, length - 1, v[i])] = v[i]; } } return length; } int main () { int n; scanf("%d", &n); vector<int64_t> cities; cities.reserve(n + 8); int64_t sum = 0; for (int i = 0; i < n; ++i) { int64_t c; scanf("%lld", &c); cities.push_back(c); sum += c; } if (sum < 0) { printf("-1\n"); return 0; } int64_t csum = 0; vector<city_t> psums; psums.push_back(make_pair(0, -1)); for (int i = 0; i < n; ++i) { csum += cities[i]; if ((csum >= 0) && (csum <= sum)) { psums.push_back(make_pair(csum, i)); } } printf("%d\n", cities.size() - lis(psums) + 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 | #include <cstdio> #include <cstdint> #include <vector> using namespace std; typedef pair<int64_t, int> city_t; int bsearch(vector<city_t>& v, int l, int r, city_t key) { while (r - l > 1) { int m = l + (r - l) / 2; if (v[m] >= key) { r = m; } else { l = m; } } return r; } int lis(vector<city_t>& v) { if (v.size() == 0) { return 0; } vector<city_t> tail(v.size(), make_pair(-10e8, 0)); int length = 1; tail[0] = v[0]; for (size_t i = 1; i < v.size(); i++) { if (v[i] < tail[0]) { tail[0] = v[i]; } else if (v[i] > tail[length - 1]) { tail[length++] = v[i]; } else { tail[bsearch(tail, -1, length - 1, v[i])] = v[i]; } } return length; } int main () { int n; scanf("%d", &n); vector<int64_t> cities; cities.reserve(n + 8); int64_t sum = 0; for (int i = 0; i < n; ++i) { int64_t c; scanf("%lld", &c); cities.push_back(c); sum += c; } if (sum < 0) { printf("-1\n"); return 0; } int64_t csum = 0; vector<city_t> psums; psums.push_back(make_pair(0, -1)); for (int i = 0; i < n; ++i) { csum += cities[i]; if ((csum >= 0) && (csum <= sum)) { psums.push_back(make_pair(csum, i)); } } printf("%d\n", cities.size() - lis(psums) + 1); return 0; } |