#include <iostream> #include <queue> #include <deque> #include <algorithm> using namespace std; typedef unsigned long UL; typedef unsigned long long ULL; #define FOR(i,a,b) for(int i=a;i<b;i++) #define MAX_POINTS 2000 #define TR(i,x) for(typeof(x.begin()) i=x.begin();i!=x.end();i++) struct Interval { int start; int len; UL val; }; struct Cmp { bool operator()(const Interval& lhs, const Interval& rhs) const { return lhs.val < rhs.val; } } myobj; std::vector<Interval> pq; int used[MAX_POINTS]; bool add_interval(int start, int len){ while (1){ if (used[start] == -1){ used[start] = len; return true; } int len1 = used[start]; if (len1 == len) return false; if (len1 > len){ start = start + len; len = len1 - len; } if (len1 < len){ start = start + len1; len = len - len1; } } } int main() { int n; ULL res = 0; Interval iv; scanf("%d",&n); FOR(i,0,n) { used[i] = -1; iv.start = i; FOR(j,i,n) { iv.len = j - i + 1; scanf("%lu",&(iv.val)); pq.push_back(iv); } } sort(pq.begin(), pq.end(), myobj); TR (iv, pq) if (add_interval(iv->start, iv->len)) res += iv->val; cout << res << endl; 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 | #include <iostream> #include <queue> #include <deque> #include <algorithm> using namespace std; typedef unsigned long UL; typedef unsigned long long ULL; #define FOR(i,a,b) for(int i=a;i<b;i++) #define MAX_POINTS 2000 #define TR(i,x) for(typeof(x.begin()) i=x.begin();i!=x.end();i++) struct Interval { int start; int len; UL val; }; struct Cmp { bool operator()(const Interval& lhs, const Interval& rhs) const { return lhs.val < rhs.val; } } myobj; std::vector<Interval> pq; int used[MAX_POINTS]; bool add_interval(int start, int len){ while (1){ if (used[start] == -1){ used[start] = len; return true; } int len1 = used[start]; if (len1 == len) return false; if (len1 > len){ start = start + len; len = len1 - len; } if (len1 < len){ start = start + len1; len = len - len1; } } } int main() { int n; ULL res = 0; Interval iv; scanf("%d",&n); FOR(i,0,n) { used[i] = -1; iv.start = i; FOR(j,i,n) { iv.len = j - i + 1; scanf("%lu",&(iv.val)); pq.push_back(iv); } } sort(pq.begin(), pq.end(), myobj); TR (iv, pq) if (add_interval(iv->start, iv->len)) res += iv->val; cout << res << endl; return 0; } |