Hdu3607--traversal

Source: Internet
Author: User

Traversal Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 1038 Accepted Submission (s): 365


Problem Descriptionxiao Ming is travelling, and today he came to the West Lake, to see people playing a game, this game is Like this, the lake placed N-box, from the 1 to N label. These boxes is floating in the water, there is some gold inside each box. Then participants from coast-to-side, each box has its own height, you can only jump from lower height to hi Gher height, and from a small label to a big label, you can skip some of the the middle of the box. Suppose the minimum height is this side and the other side have the maximum height. Xiao Ming would what do you get the most gold? He now needs your help.
Inputthere is multiple test cases. Each test case contains one integer N, representing the number of boxes. The following N lines each line contains the integers hi and gi, indicate the height and the number of the gold of the ith bo X.
1 < = N < 100 001
0 < Hi < 100 000 001
0 < GI < = 10000

Outputfor Each test case you should output a, containing the number of maximum gold xiaoming can get.


Sample Input
41 12 22 35 11 1 10000

Sample Output
510000

Source2010 acm-icpc multi-university Training Contest (+)--host by Zstu
Recommendlcy | We have carefully selected several similar problems for you:3397 3525 1698 1542 2871

Simple line-Tree DP
#include <map> #include <set> #include <list> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring># Include <iostream> #include <algorithm>using namespace std;const int N = 200010;const int inf = -0x3f3f3f3f;in T dp[n];int xis[n];int N, cnt;struct node{int L, R;int Val;} Tree[n << 2];struct node2{int g;int H;}  Box[n];int binsearch (int val) {int L = 1, r = cnt, Mid;int ans;while (l <= r) {mid = (L + R) >> 1;if (Xis[mid] > val) {r = mid-1;} else if (Xis[mid] < val) {L = mid + 1;} Else{ans = Mid;break;}} return ans;} void build (int p, int l, int r) {tree[p].l = L;TREE[P].R = R;tree[p].val = inf;if (L = = r) {return;} int mid = (L + R) >> 1;build (P << 1, L, mid); build (P << 1 | 1, mid + 1, r);} void Update (int p, int pos, int val) {if (tree[p].l = = TREE[P].R) {tree[p].val = Val;return;} int mid = (tree[p].l + tree[p].r) >> 1;if (POS <= mid) {Update (P << 1, POS, val);} Else{update (P << 1 | 1, POS, val);} Tree[p].val = max (tree[p << 1].val, tree[p << 1 | 1].val);} int query (int p, int l, int r) {if (L <= tree[p].l && TREE[P].R <= r) {return tree[p].val;} int mid = (tree[p].l + tree[p].r) >> 1;if (R <= mid) {return query (P << 1, L, r);} else if (L > Mid) {return query (P << 1 | 1, L, r);} Else{return max (Query (P << 1, L, mid), query (P << 1 | 1, mid + 1, r));}} int main () {while (~SCANF ("%d", &n)) {int ans = 0;cnt = 1;xis[0] = 0;for (int i = 1; I <= n; ++i) {scanf ("%d%d", & Box[i].h, &AMP;BOX[I].G); xis[++cnt] = box[i].h;} Sort (XIs, XIs + cnt + 1), cnt = unique (XIs, XIs + cnt + 1)-Xis-1;build (1, 1, CNT);DP [0] = 0;update (1, 1, dp[0]); for (i NT i = 1; I <= N; ++i) {Int J = binsearch (box[i].h), last;if (j = = 1) {Dp[i] = BOX[I].G;} Else{last = Query (1, 1, j-1);DP [i] = last + box[i].g;} Update (1, J, Dp[i]); ans = max (ans, dp[i]);} printf ("%d\n", ans);}return 0;} 


Hdu3607--traversal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.