#include <algorithm> #include <cstdio> #include <utility> #include <vector> #define COST first #define VALUE second using namespace std; struct state { int cost0 = 0; vector<pair<int, int> > options; }; int input[500001]; state states[2]; void update(int value, state& prevState, state& currState) { pair<int, int> tmp; vector<pair<int, int> > v; if (prevState.cost0 >= 0) { // don't connect back v.push_back(make_pair(prevState.cost0, value)); } for (int i = 0; i < prevState.options.size(); i++) { tmp = prevState.options[i]; tmp.COST++; tmp.VALUE += value; v.push_back(tmp); } sort(v.begin(), v.end()); currState.cost0 = -1; currState.options.clear(); int bestValue = 0xf0000000; for (int i = 0; i < v.size(); i++) { if (v[i].VALUE > bestValue) { currState.options.push_back(v[i]); bestValue = v[i].VALUE; } } for (int i = 0; i < v.size(); i++) { if (currState.options[i].VALUE >= 0) { currState.cost0 = currState.options[i].COST; break; } } // printf("(%d) %d: c0: %d\n", prevState.cost0, value, currState.cost0); // for (int i = 0; i < currState.options.size(); i++) { // printf(" c:%d v:%d\n", currState.options[i].COST, currState.options[i].VALUE); // } } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &input[i]); } for (int i = 1; i <= n; i++) { update(input[i], states[(i + 1) & 1], states[i & 1]); } printf("%d\n", states[n & 1].cost0); 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 | #include <algorithm> #include <cstdio> #include <utility> #include <vector> #define COST first #define VALUE second using namespace std; struct state { int cost0 = 0; vector<pair<int, int> > options; }; int input[500001]; state states[2]; void update(int value, state& prevState, state& currState) { pair<int, int> tmp; vector<pair<int, int> > v; if (prevState.cost0 >= 0) { // don't connect back v.push_back(make_pair(prevState.cost0, value)); } for (int i = 0; i < prevState.options.size(); i++) { tmp = prevState.options[i]; tmp.COST++; tmp.VALUE += value; v.push_back(tmp); } sort(v.begin(), v.end()); currState.cost0 = -1; currState.options.clear(); int bestValue = 0xf0000000; for (int i = 0; i < v.size(); i++) { if (v[i].VALUE > bestValue) { currState.options.push_back(v[i]); bestValue = v[i].VALUE; } } for (int i = 0; i < v.size(); i++) { if (currState.options[i].VALUE >= 0) { currState.cost0 = currState.options[i].COST; break; } } // printf("(%d) %d: c0: %d\n", prevState.cost0, value, currState.cost0); // for (int i = 0; i < currState.options.size(); i++) { // printf(" c:%d v:%d\n", currState.options[i].COST, currState.options[i].VALUE); // } } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &input[i]); } for (int i = 1; i <= n; i++) { update(input[i], states[(i + 1) & 1], states[i & 1]); } printf("%d\n", states[n & 1].cost0); return 0; } |