#include <bits/stdc++.h> #include <unistd.h> #include <sstream> using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; typedef long long LL; typedef unsigned long long ULL; #define BIGMOD 1000012177LL #define INF 1000000000 struct Building { LL mx; int pos; // pozycje na glownej liscie //int sameTypeLeft, sameTypeRight; //int otherTypeLeft, otherTypeRight; // na wewnetrznych listach: elektrownie i fabryki //int idxOnList; //int otherListIdx; int bestScore; Building(LL v, int p) : mx(v) , pos(p) , bestScore(0) //, sameTypeLeft(-1) //, sameTypeRight(-1) //, otherTypeLeft(-1) //, otherTypeRight(-1) //, idxOnList(0) //, otherListIdx(0) {} /*string toString() { ostringstream oss; char t = mx < 0 ? 'C' : 'P'; //oss << t << "(p=" << pos << ",mx=" << mx << ")"; //oss << t << "(p=" << pos << "," << used << "/" << mx << ",[" << sameTypeLeft << "," << sameTypeRight << "],[" << otherTypeLeft << "," << otherTypeRight << "])"; return oss.str(); }*/ }; void selftest() { //solveSlow({2,-5,0,2,0,0,0,4,0,0,-1,4,0,0,0,0,-3}); //solveSlow({5,5,0,0,0,-10,0,0,0,-1,10,1}); } /* void calculateNeighbours(vector<Building> &buildings, const vector<Building> &elektrownie, const vector<Building> &fabryki) { int eIdx = 0; for (int i=0; i < buildings.size(); i++) { Building &b = buildings[i]; if (b.mx > 0) { int prev = b.idxOnList - 1; int next = b.idxOnList + 1; b.sameTypeLeft = (prev >= 0) ? elektrownie[prev].pos : -1; b.sameTypeRight = (next < elektrownie.size()) ? elektrownie[next].pos : -1; } if (b.mx < 0) { int prev = b.idxOnList - 1; int next = b.idxOnList + 1; b.sameTypeLeft = (prev >= 0) ? fabryki[prev].pos : -1; b.sameTypeRight = (next < fabryki.size()) ? fabryki[next].pos : -1; } } for (int i=0; i < buildings.size(); i++) { Building &b = buildings[i]; if (b.mx > 0) { int prev = b.otherListIdx - 1; int next = prev + 1; b.otherTypeLeft = (prev >= 0) ? fabryki[prev].pos : -1; b.otherTypeRight = (next < fabryki.size()) ? fabryki[next].pos : -1; } if (b.mx < 0) { int prev = b.otherListIdx - 1; int next = prev + 1; b.otherTypeLeft = (prev >= 0) ? elektrownie[prev].pos : -1; b.otherTypeRight = (next < elektrownie.size()) ? elektrownie[next].pos : -1; } } } */ LL solve(vector<Building> &buildings) { if (buildings.size() < 1) { return 0; } vector<LL> prefixSum(buildings.size() + 1); prefixSum[0] = 0; for (int i=1; i <= buildings.size(); i++) { prefixSum[i] = prefixSum[i-1] + buildings[i-1].mx; //printf("%lld ", prefixSum[i]); } if (prefixSum[buildings.size()] < 0) { return -1; } //printf("\n"); for (int i=0; i < buildings.size(); i++) { Building &b = buildings[i]; //printf("\n%s \n", b.toString().c_str()); if (prefixSum[i+1] < 0) { b.bestScore = INF; //printf("prefixSum %lld below zero, skipping\n", prefixSum[i+1]); continue; } int withCurrentBest = INF; for (int k=i; k >= 0; --k) { Building &middle = buildings[k]; int dist = b.pos - middle.pos; if (dist > withCurrentBest) { break; } if (prefixSum[i+1] - prefixSum[k] >= 0) { //printf("prefixSum allows better result at k=%d\n", k); int rightHalf = dist; int leftHalf = 0; if (k-1 >= 0) { leftHalf = buildings[k-1].bestScore; } //printf("leftHalf=%d,rightHalf=%d\n", leftHalf, rightHalf); withCurrentBest = min(withCurrentBest, leftHalf + rightHalf); //printf("found better K at pos %d\n", middle.pos); } } b.bestScore = withCurrentBest; //printf("bestScore=%d\n", b.bestScore); } return buildings[buildings.size() -1 ].bestScore; } int main() { int n; selftest(); scanf("%d", &n); vector<Building> elektrownie; vector<Building> fabryki; vector<Building> buildings; for (int i=0; i < n; i++) { LL x; scanf("%lld", &x); Building b(x,i); if (x > 0) { //b.idxOnList = elektrownie.size(); //b.otherListIdx = fabryki.size(); elektrownie.emplace_back(b); } if (x < 0) { //b.idxOnList = fabryki.size(); //b.otherListIdx = elektrownie.size(); fabryki.emplace_back(b); } if (x) buildings.emplace_back(b); } //calculateNeighbours(buildings, elektrownie, fabryki); //printf("total buildings: %d\n", buildings.size()); LL answer = solve(buildings); printf("%lld\n", answer); 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | #include <bits/stdc++.h> #include <unistd.h> #include <sstream> using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; typedef long long LL; typedef unsigned long long ULL; #define BIGMOD 1000012177LL #define INF 1000000000 struct Building { LL mx; int pos; // pozycje na glownej liscie //int sameTypeLeft, sameTypeRight; //int otherTypeLeft, otherTypeRight; // na wewnetrznych listach: elektrownie i fabryki //int idxOnList; //int otherListIdx; int bestScore; Building(LL v, int p) : mx(v) , pos(p) , bestScore(0) //, sameTypeLeft(-1) //, sameTypeRight(-1) //, otherTypeLeft(-1) //, otherTypeRight(-1) //, idxOnList(0) //, otherListIdx(0) {} /*string toString() { ostringstream oss; char t = mx < 0 ? 'C' : 'P'; //oss << t << "(p=" << pos << ",mx=" << mx << ")"; //oss << t << "(p=" << pos << "," << used << "/" << mx << ",[" << sameTypeLeft << "," << sameTypeRight << "],[" << otherTypeLeft << "," << otherTypeRight << "])"; return oss.str(); }*/ }; void selftest() { //solveSlow({2,-5,0,2,0,0,0,4,0,0,-1,4,0,0,0,0,-3}); //solveSlow({5,5,0,0,0,-10,0,0,0,-1,10,1}); } /* void calculateNeighbours(vector<Building> &buildings, const vector<Building> &elektrownie, const vector<Building> &fabryki) { int eIdx = 0; for (int i=0; i < buildings.size(); i++) { Building &b = buildings[i]; if (b.mx > 0) { int prev = b.idxOnList - 1; int next = b.idxOnList + 1; b.sameTypeLeft = (prev >= 0) ? elektrownie[prev].pos : -1; b.sameTypeRight = (next < elektrownie.size()) ? elektrownie[next].pos : -1; } if (b.mx < 0) { int prev = b.idxOnList - 1; int next = b.idxOnList + 1; b.sameTypeLeft = (prev >= 0) ? fabryki[prev].pos : -1; b.sameTypeRight = (next < fabryki.size()) ? fabryki[next].pos : -1; } } for (int i=0; i < buildings.size(); i++) { Building &b = buildings[i]; if (b.mx > 0) { int prev = b.otherListIdx - 1; int next = prev + 1; b.otherTypeLeft = (prev >= 0) ? fabryki[prev].pos : -1; b.otherTypeRight = (next < fabryki.size()) ? fabryki[next].pos : -1; } if (b.mx < 0) { int prev = b.otherListIdx - 1; int next = prev + 1; b.otherTypeLeft = (prev >= 0) ? elektrownie[prev].pos : -1; b.otherTypeRight = (next < elektrownie.size()) ? elektrownie[next].pos : -1; } } } */ LL solve(vector<Building> &buildings) { if (buildings.size() < 1) { return 0; } vector<LL> prefixSum(buildings.size() + 1); prefixSum[0] = 0; for (int i=1; i <= buildings.size(); i++) { prefixSum[i] = prefixSum[i-1] + buildings[i-1].mx; //printf("%lld ", prefixSum[i]); } if (prefixSum[buildings.size()] < 0) { return -1; } //printf("\n"); for (int i=0; i < buildings.size(); i++) { Building &b = buildings[i]; //printf("\n%s \n", b.toString().c_str()); if (prefixSum[i+1] < 0) { b.bestScore = INF; //printf("prefixSum %lld below zero, skipping\n", prefixSum[i+1]); continue; } int withCurrentBest = INF; for (int k=i; k >= 0; --k) { Building &middle = buildings[k]; int dist = b.pos - middle.pos; if (dist > withCurrentBest) { break; } if (prefixSum[i+1] - prefixSum[k] >= 0) { //printf("prefixSum allows better result at k=%d\n", k); int rightHalf = dist; int leftHalf = 0; if (k-1 >= 0) { leftHalf = buildings[k-1].bestScore; } //printf("leftHalf=%d,rightHalf=%d\n", leftHalf, rightHalf); withCurrentBest = min(withCurrentBest, leftHalf + rightHalf); //printf("found better K at pos %d\n", middle.pos); } } b.bestScore = withCurrentBest; //printf("bestScore=%d\n", b.bestScore); } return buildings[buildings.size() -1 ].bestScore; } int main() { int n; selftest(); scanf("%d", &n); vector<Building> elektrownie; vector<Building> fabryki; vector<Building> buildings; for (int i=0; i < n; i++) { LL x; scanf("%lld", &x); Building b(x,i); if (x > 0) { //b.idxOnList = elektrownie.size(); //b.otherListIdx = fabryki.size(); elektrownie.emplace_back(b); } if (x < 0) { //b.idxOnList = fabryki.size(); //b.otherListIdx = elektrownie.size(); fabryki.emplace_back(b); } if (x) buildings.emplace_back(b); } //calculateNeighbours(buildings, elektrownie, fabryki); //printf("total buildings: %d\n", buildings.size()); LL answer = solve(buildings); printf("%lld\n", answer); return 0; } |