#include <algorithm> #include <cstdio> using namespace std; #undef DEBUG #define SUMA_CIAGU_ARYTMETYCZNEGO(n) (((n + 1) * n) / 2) const int MAX_N = 2000; const int MAX_C = 1e9; typedef struct { int i, j; int cost; } range_t; typedef struct { int index; range_t *range; int o_cnt; // number of ranges opening here int c_cnt; // number of ranges closing here } kubek_t; bool sortKubekPtrCostDesc(kubek_t *i, kubek_t *j) { return i->range->cost > j->range->cost; } bool sortRangesCostDescLengthAscStartAsc(range_t *a, range_t *b) { if (a->cost == b->cost) { if ((a->j - a->i) == (b->j - b->i)) { return a->i < b->i; } else return (a->j - a->i) < (b->j - b->i); } else return a->cost > b->cost; } bool sortRangesCostDescLengthDescStartAsc(range_t *a, range_t *b) { if (a->cost == b->cost) { if ((a->j - a->i) == (b->j - b->i)) { return a->i < b->i; } else return (a->j - a->i) > (b->j - b->i); } else return a->cost > b->cost; } bool sortRangesCostAscLengthAscStartAsc(range_t *a, range_t *b) { if (a->cost == b->cost) { if ((a->j - a->i) == (b->j - b->i)) { return a->i < b->i; } else return (a->j - a->i) < (b->j - b->i); } else return a->cost < b->cost; } bool sortRangesLengthAscCostAscStartAsc(range_t *a, range_t *b) { if ((a->j - a->i) == (b->j - b->i)) { if (a->cost == b->cost) { return a->i < b->i; } else return a->cost < b->cost; } else return (a->j - a->i) < (b->j - b->i); } range_t r[MAX_N][MAX_N]; range_t* rPtr[SUMA_CIAGU_ARYTMETYCZNEGO(MAX_N)]; kubek_t k[MAX_N]; kubek_t* kCandidates[MAX_N]; kubek_t* kPtr[MAX_N]; int getCandidates(const range_t& range, kubek_t* candidates[]) { int candidateCount = 0; for (int x = range.i; x <= range.j; x++) { if (range.cost < k[x].range->cost) { candidates[candidateCount++] = &k[x]; } } sort(candidates, candidates + candidateCount, sortKubekPtrCostDesc); return candidateCount; } bool canUseRangeFor(int n, const range_t& range, const kubek_t& candidate) { // verify range to be removed const range_t* rToBeRemoved = candidate.range; // if we're removing range start, then previous cup must have range end if (rToBeRemoved->i != 0 && rToBeRemoved->i != range.i && k[rToBeRemoved->i].o_cnt == 1 && k[rToBeRemoved->i - 1].c_cnt == 0) { #ifdef DEBUG printf("cup [%d]: not using range [%d -> %d] due to missing closing on cup [%d]\n", candidate.index, range.i, range.j, rToBeRemoved->i - 1); printf("cup [%d]: range to be removed [%d -> %d]\n", candidate.index, rToBeRemoved->i, rToBeRemoved->j); #endif return false; } // if we're removing range end, then next cup must have range start if (rToBeRemoved->j != n - 1 && rToBeRemoved->j != range.j && k[rToBeRemoved->j].c_cnt == 1 && k[rToBeRemoved->j + 1].o_cnt == 0) { #ifdef DEBUG printf("cup [%d]: not using range [%d -> %d] due to missing opening on cup [%d]\n", candidate.index, range.i, range.j, rToBeRemoved->j + 1); printf("cup [%d]: range to be removed [%d -> %d]\n", candidate.index, rToBeRemoved->i, rToBeRemoved->j); #endif return false; } return true; } void optimalCost(int n) { // first unoptimal cost (all ranges of length 1) for (int i = 0; i < n; k[i].index = i, k[i].range = &r[i][i], k[i].o_cnt = 1, k[i].c_cnt = 1, i++); // sort ranges int rangeCount = SUMA_CIAGU_ARYTMETYCZNEGO(n); sort(rPtr, rPtr + rangeCount, sortRangesCostAscLengthAscStartAsc); for (int x = 0; x < rangeCount; x++) { range_t* newRange = rPtr[x]; // skip 1-element ranges if (newRange->i == newRange->j) continue; int candidateCount = getCandidates(*newRange, kCandidates); #ifdef DEBUG printf("range [%d -> %d], cost [%d], candidates [%d]\n", newRange->i, newRange->j, newRange->cost, candidateCount); #endif for (int y = 0; y < candidateCount; y++) { kubek_t* candidate = kCandidates[y]; if (canUseRangeFor(n, *newRange, *candidate)) { // not using old range anymore range_t* oldRange = candidate->range; range_t* newRange = rPtr[x]; #ifdef DEBUG printf("cup [%d]: removing range [%d -> %d]\n", candidate->index, oldRange->i, oldRange->j); #endif k[oldRange->i].o_cnt--; k[oldRange->j].c_cnt--; #ifdef DEBUG printf("cup [%d]: using range [%d -> %d]\n", candidate->index, newRange->i, newRange->j); #endif candidate->range = newRange; k[newRange->i].o_cnt++; k[newRange->j].c_cnt++; break; } } } } int main() { // input int n; scanf("%d", &n); for (int i = 0, l = 0; i < n; i++) { for (int j = i; j < n; j++) { r[i][j].i = i; r[i][j].j = j; scanf("%d", &r[i][j].cost); rPtr[l++] = &r[i][j]; } } // calculate optimalCost(n); // output long long totalCost = 0; for (int i = 0; i < n; totalCost += k[i++].range->cost); printf("%lld\n", totalCost); 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 198 199 200 201 202 203 204 205 206 | #include <algorithm> #include <cstdio> using namespace std; #undef DEBUG #define SUMA_CIAGU_ARYTMETYCZNEGO(n) (((n + 1) * n) / 2) const int MAX_N = 2000; const int MAX_C = 1e9; typedef struct { int i, j; int cost; } range_t; typedef struct { int index; range_t *range; int o_cnt; // number of ranges opening here int c_cnt; // number of ranges closing here } kubek_t; bool sortKubekPtrCostDesc(kubek_t *i, kubek_t *j) { return i->range->cost > j->range->cost; } bool sortRangesCostDescLengthAscStartAsc(range_t *a, range_t *b) { if (a->cost == b->cost) { if ((a->j - a->i) == (b->j - b->i)) { return a->i < b->i; } else return (a->j - a->i) < (b->j - b->i); } else return a->cost > b->cost; } bool sortRangesCostDescLengthDescStartAsc(range_t *a, range_t *b) { if (a->cost == b->cost) { if ((a->j - a->i) == (b->j - b->i)) { return a->i < b->i; } else return (a->j - a->i) > (b->j - b->i); } else return a->cost > b->cost; } bool sortRangesCostAscLengthAscStartAsc(range_t *a, range_t *b) { if (a->cost == b->cost) { if ((a->j - a->i) == (b->j - b->i)) { return a->i < b->i; } else return (a->j - a->i) < (b->j - b->i); } else return a->cost < b->cost; } bool sortRangesLengthAscCostAscStartAsc(range_t *a, range_t *b) { if ((a->j - a->i) == (b->j - b->i)) { if (a->cost == b->cost) { return a->i < b->i; } else return a->cost < b->cost; } else return (a->j - a->i) < (b->j - b->i); } range_t r[MAX_N][MAX_N]; range_t* rPtr[SUMA_CIAGU_ARYTMETYCZNEGO(MAX_N)]; kubek_t k[MAX_N]; kubek_t* kCandidates[MAX_N]; kubek_t* kPtr[MAX_N]; int getCandidates(const range_t& range, kubek_t* candidates[]) { int candidateCount = 0; for (int x = range.i; x <= range.j; x++) { if (range.cost < k[x].range->cost) { candidates[candidateCount++] = &k[x]; } } sort(candidates, candidates + candidateCount, sortKubekPtrCostDesc); return candidateCount; } bool canUseRangeFor(int n, const range_t& range, const kubek_t& candidate) { // verify range to be removed const range_t* rToBeRemoved = candidate.range; // if we're removing range start, then previous cup must have range end if (rToBeRemoved->i != 0 && rToBeRemoved->i != range.i && k[rToBeRemoved->i].o_cnt == 1 && k[rToBeRemoved->i - 1].c_cnt == 0) { #ifdef DEBUG printf("cup [%d]: not using range [%d -> %d] due to missing closing on cup [%d]\n", candidate.index, range.i, range.j, rToBeRemoved->i - 1); printf("cup [%d]: range to be removed [%d -> %d]\n", candidate.index, rToBeRemoved->i, rToBeRemoved->j); #endif return false; } // if we're removing range end, then next cup must have range start if (rToBeRemoved->j != n - 1 && rToBeRemoved->j != range.j && k[rToBeRemoved->j].c_cnt == 1 && k[rToBeRemoved->j + 1].o_cnt == 0) { #ifdef DEBUG printf("cup [%d]: not using range [%d -> %d] due to missing opening on cup [%d]\n", candidate.index, range.i, range.j, rToBeRemoved->j + 1); printf("cup [%d]: range to be removed [%d -> %d]\n", candidate.index, rToBeRemoved->i, rToBeRemoved->j); #endif return false; } return true; } void optimalCost(int n) { // first unoptimal cost (all ranges of length 1) for (int i = 0; i < n; k[i].index = i, k[i].range = &r[i][i], k[i].o_cnt = 1, k[i].c_cnt = 1, i++); // sort ranges int rangeCount = SUMA_CIAGU_ARYTMETYCZNEGO(n); sort(rPtr, rPtr + rangeCount, sortRangesCostAscLengthAscStartAsc); for (int x = 0; x < rangeCount; x++) { range_t* newRange = rPtr[x]; // skip 1-element ranges if (newRange->i == newRange->j) continue; int candidateCount = getCandidates(*newRange, kCandidates); #ifdef DEBUG printf("range [%d -> %d], cost [%d], candidates [%d]\n", newRange->i, newRange->j, newRange->cost, candidateCount); #endif for (int y = 0; y < candidateCount; y++) { kubek_t* candidate = kCandidates[y]; if (canUseRangeFor(n, *newRange, *candidate)) { // not using old range anymore range_t* oldRange = candidate->range; range_t* newRange = rPtr[x]; #ifdef DEBUG printf("cup [%d]: removing range [%d -> %d]\n", candidate->index, oldRange->i, oldRange->j); #endif k[oldRange->i].o_cnt--; k[oldRange->j].c_cnt--; #ifdef DEBUG printf("cup [%d]: using range [%d -> %d]\n", candidate->index, newRange->i, newRange->j); #endif candidate->range = newRange; k[newRange->i].o_cnt++; k[newRange->j].c_cnt++; break; } } } } int main() { // input int n; scanf("%d", &n); for (int i = 0, l = 0; i < n; i++) { for (int j = i; j < n; j++) { r[i][j].i = i; r[i][j].j = j; scanf("%d", &r[i][j].cost); rPtr[l++] = &r[i][j]; } } // calculate optimalCost(n); // output long long totalCost = 0; for (int i = 0; i < n; totalCost += k[i++].range->cost); printf("%lld\n", totalCost); return 0; } |