中级农民
- 积分
- 133
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2011-3-5
- 最后登录
- 1970-1-1
|
本帖最后由 Imbalism 于 2011-4-20 21:12 编辑
我的代码, 方法如前楼所述
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <limits>
- #include <assert.h>
- using namespace std;
- const int MIN = numeric_limits<int>::min();
- struct Node
- {
- int start;
- int end;
- int value;
- };
- bool compare(const Node& a, const Node& b)
- {
- return a.end < b.end;
- }
- int max(int a, int b)
- {
- return a > b ? a : b;
- }
- //if t is not found in the array a
- //return the index of nearest element
- //that is smaller than t
- int binarySearch(vector<Node> &a, int l, int u, int t)
- {
- while(true)
- {
- int m = (l + u + 1) / 2;
- if(a[m].end == t)
- return m;
- if(a[m].end < t)
- {
- //t is in (m + 1, u)
- l = m + 1;
- if (l > u)
- assert(a.end < t);
- return u;
- }
- if(a[m].end > t)
- {
- //t is in (l, m - 1)
- u = m - 1;
- if(l > u)
- assert(a[l].end < t);
- return l;
- }
- }
- }
- int maxSum(vector<Node> &a)
- {
- //dummy node for coding convenience
- Node dummyMin = {MIN, MIN, 0};
- a.push_back(dummyMin);
- sort(a.begin(), a.end(), compare);
- int *dp = new int[a.size()];
- dp[0] = 0;
- for (int i = 1; i < a.size(); ++i)
- {
- //if i is not in the optimized solution
- int v1 = dp[i - 1];
- //if i is in the optimized solution
- int k = binarySearch(a, 0, i - 1, a.start);
- assert(k < i && k >= 0);
- assert(a[k + 1].end > a.start && a[k].end <= a.start);
- int v2 = dp[k] + a.value;
- //find the max of two cases
- dp = max(v1, v2);
- assert(dp >= dp[i - 1]);
- }
- int r = dp[a.size() - 1];
- delete[] dp;
- return r;
- }
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <limits>
- #include <assert.h>
- using namespace std;
- const int MIN = numeric_limits<int>::min();
- struct Node
- {
- int start;
- int end;
- int value;
- };
- bool compare(const Node& a, const Node& b)
- {
- return a.end < b.end;
- }
- int max(int a, int b)
- {
- return a > b ? a : b;
- }
- //if t is not found in the array a
- //return the index of nearest element
- //that is smaller than t
- int binarySearch(vector<Node> &a, int l, int u, int t)
- {
- while(true)
- {
- int m = (l + u + 1) / 2;
- if(a[m].end == t)
- return m;
- if(a[m].end < t)
- {
- //t is in (m + 1, u)
- l = m + 1;
- if (l > u)
- assert(a.end < t);
- return u;
- }
- if(a[m].end > t)
- {
- //t is in (l, m - 1)
- u = m - 1;
- if(l > u)
- assert(a[l].end < t);
- return l;
- }
- }
- }
- int maxSum(vector<Node> &a)
- {
- //dummy node for coding convenience
- Node dummyMin = {MIN, MIN, 0};
- a.push_back(dummyMin);
- sort(a.begin(), a.end(), compare);
- int *dp = new int[a.size()];
- dp[0] = 0;
- for (int i = 1; i < a.size(); ++i)
- {
- //if i is not in the optimized solution
- int v1 = dp[i - 1];
- //if i is in the optimized solution
- int k = binarySearch(a, 0, i - 1, a.start);
- assert(k < i && k >= 0);
- assert(a[k + 1].end > a.start && a[k].end <= a.start);
- int v2 = dp[k] + a.value;
- //find the max of two cases
- dp = max(v1, v2);
- assert(dp >= dp[i - 1]);
- }
- int r = dp[a.size() - 1];
- int main(int argc, char *argv[])
- {
- vector<Node> a = {{1, 3, 3}, {2, 3, 100}, {0, 4, 2}, {3,5, 7}, {2, 6,1}, {0, 6, 2}, {2, 6, 1}, {6, 7, 3}};
- cout << maxSum(a) << endl;
- return 0;
- }
复制代码
|
|