#include <iostream> #include <stdlib.h> #include <vector> using namespace std; int countLen(vector<int> vec, int start, int factor, int &end) { int len = 0, sum = vec[factor]; for (int i = start; i <= end; i++) { if (vec[i] > 0) { sum += vec[i]; } len++; if (sum>=0) { if (i < factor) { len += factor - i; end = factor; } else end = i; break; } } if (sum < 0) return -1; else return len-1; } int subvec(vector<int> &vec,int start,int factor,int end) { int len = -1, newLen, tempEnd, bestStart, bestEnd, change = vec[factor]; for (int i = start; i <= factor; i++) { if (vec[i] > 0 || i==factor) { tempEnd = end; newLen=countLen(vec, i, factor, tempEnd); if (len == -1) { len = newLen; bestStart = i; bestEnd = tempEnd; } else if (len > newLen && newLen!=-1) { len = newLen; bestStart = i; bestEnd = tempEnd; } } } if (len == -1) return 0; else { for (int i = bestStart; i <= bestEnd; i++) { if (vec[i] > 0) { if (change + vec[i] < 0) { change += vec[i]; vec[i] = 0; } else { vec[i] = vec[i]+change; } } } return len; } } int main() { int n, x, sum=0; vector<int> vec; cin >> n; for (int i = 0; i < n; i++) { cin >> x; vec.push_back(x); } int start=0, factor=-1, end; for (int i = 0; i < n; i++) { if (vec[i] < 0){ if (factor != -1){ end = i - 1; sum += subvec(vec, start, factor, end); start = factor + 1; factor = i; } else { factor = i; } } } end = n - 1; sum += subvec(vec, start, factor, end); if (sum == 0) sum = -1; cout << sum; system("pause"); 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 | #include <iostream> #include <stdlib.h> #include <vector> using namespace std; int countLen(vector<int> vec, int start, int factor, int &end) { int len = 0, sum = vec[factor]; for (int i = start; i <= end; i++) { if (vec[i] > 0) { sum += vec[i]; } len++; if (sum>=0) { if (i < factor) { len += factor - i; end = factor; } else end = i; break; } } if (sum < 0) return -1; else return len-1; } int subvec(vector<int> &vec,int start,int factor,int end) { int len = -1, newLen, tempEnd, bestStart, bestEnd, change = vec[factor]; for (int i = start; i <= factor; i++) { if (vec[i] > 0 || i==factor) { tempEnd = end; newLen=countLen(vec, i, factor, tempEnd); if (len == -1) { len = newLen; bestStart = i; bestEnd = tempEnd; } else if (len > newLen && newLen!=-1) { len = newLen; bestStart = i; bestEnd = tempEnd; } } } if (len == -1) return 0; else { for (int i = bestStart; i <= bestEnd; i++) { if (vec[i] > 0) { if (change + vec[i] < 0) { change += vec[i]; vec[i] = 0; } else { vec[i] = vec[i]+change; } } } return len; } } int main() { int n, x, sum=0; vector<int> vec; cin >> n; for (int i = 0; i < n; i++) { cin >> x; vec.push_back(x); } int start=0, factor=-1, end; for (int i = 0; i < n; i++) { if (vec[i] < 0){ if (factor != -1){ end = i - 1; sum += subvec(vec, start, factor, end); start = factor + 1; factor = i; } else { factor = i; } } } end = n - 1; sum += subvec(vec, start, factor, end); if (sum == 0) sum = -1; cout << sum; system("pause"); return 0; } |