#include <cstdio> #include <vector> #include <algorithm> #include <functional> using namespace std; typedef long long LL; int main() { int n; scanf("%d", &n); vector<int> a; a.resize(n); for (int i=0; i<n; ++i) { scanf("%d", &a[i]); } vector<LL> sum; sum.resize(n+1); sum[0] = 0; for (int i=1; i<=n; ++i) { sum[i] = sum[i-1] + a[i-1]; } if (sum[n] < 0) { printf("-1\n"); return 0; } // Remembers energy taken per lines saved. vector<LL> optimal; optimal.push_back(0); for (int i=0; i<n; ++i) { LL energy = sum[i+1]; if (energy < 0) continue; if (energy > sum[n]) continue; auto it = upper_bound(optimal.begin(), optimal.end(), energy); // printf("Can remove %d at %d and save %d\n", energy, i, it-optimal.begin()); if (it == optimal.end()) { optimal.push_back(sum[i+1]); } else { *it = energy; } } printf("%lld\n", n + 1 - static_cast<LL>(optimal.size())); 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 | #include <cstdio> #include <vector> #include <algorithm> #include <functional> using namespace std; typedef long long LL; int main() { int n; scanf("%d", &n); vector<int> a; a.resize(n); for (int i=0; i<n; ++i) { scanf("%d", &a[i]); } vector<LL> sum; sum.resize(n+1); sum[0] = 0; for (int i=1; i<=n; ++i) { sum[i] = sum[i-1] + a[i-1]; } if (sum[n] < 0) { printf("-1\n"); return 0; } // Remembers energy taken per lines saved. vector<LL> optimal; optimal.push_back(0); for (int i=0; i<n; ++i) { LL energy = sum[i+1]; if (energy < 0) continue; if (energy > sum[n]) continue; auto it = upper_bound(optimal.begin(), optimal.end(), energy); // printf("Can remove %d at %d and save %d\n", energy, i, it-optimal.begin()); if (it == optimal.end()) { optimal.push_back(sum[i+1]); } else { *it = energy; } } printf("%lld\n", n + 1 - static_cast<LL>(optimal.size())); return 0; } |