HDU 6447 YJJ's Salesman 【线段树】【dp】【离散化】

YJJ's Salesman

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1109    Accepted Submission(s): 378


 

Problem Description

YJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination.
One day, he is going to travel from city A to southeastern city B. Let us assume that A is (0,0) on the rectangle map and B (109,109). YJJ is so busy so he never turn back or go twice the same way, he will only move to east, south or southeast, which means, if YJJ is at (x,y) now (0≤x≤109,0≤y≤109), he will only forward to (x+1,y), (x,y+1) or (x+1,y+1).
On the rectangle map from (0,0) to (109,109), there are several villages scattering on the map. Villagers will do business deals with salesmen from northwestern, but not northern or western. In mathematical language, this means when there is a village k on (xk,yk) (1≤xk≤109,1≤yk≤109), only the one who was from (xk−1,yk−1) to (xk,yk) will be able to earn vk dollars.(YJJ may get different number of dollars from different village.)
YJJ has no time to plan the path, can you help him to find maximum of dollars YJJ can get.

 

Input

The first line of the input contains an integer T (1≤T≤10),which is the number of test cases.

In each case, the first line of the input contains an integer N (1≤N≤105).The following N lines, the k-th line contains 3 integers, xk,yk,vk (0≤vk≤103), which indicate that there is a village on (xk,yk) and he can get vk dollars in that village.
The positions of each village is distinct.

 

Output

The maximum of dollars YJJ can get.

 

Sample Input

 

1 3 1 1 1 1 2 2 3 3 1

 

Sample Output

 

3

#include<bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 7;
int a[MAX << 2];
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
struct node{
    int x, y, w;
    bool operator < (const node &a) const{
        return x == a.x ? y > a.y : x < a.x;
    }
} A[MAX];
void pushup(int rt){
    a[rt] = max(a[rt << 1], a[rt << 1 | 1]);
}
void update(int rt, int l, int r, int pos, int v){
    if(l == r){
        a[rt] = v;
        return;
    }
    int mid = (l + r) >> 1;
    if(pos <= mid) update(lson, pos, v);
    else update(rson, pos, v);
    pushup(rt);
}
int query(int rt, int l, int r, int x, int y){
    if(x <= l && r <= y) return a[rt];
    int mid = (l + r) >> 1;
    int ans = -111111;
    if(x <= mid) ans = max(ans, query(lson, x, y));
    if(mid < y) ans = max(ans, query(rson, x, y));
    return ans;
}
int X[MAX], Y[MAX];
int main(){
    int N;
    for(scanf("%d", &N); N; N--){
        int n;
        scanf("%d", &n);
        memset(X, 0, sizeof X);
        memset(Y, 0, sizeof Y);
        memset(a, 0, sizeof a);
        for(int i = 0; i < n; i++){
            scanf("%d%d%d", &A[i].x, &A[i].y, &A[i].w);
            X[i] = A[i].x, Y[i] = A[i].y;
        }
        sort(X, X + n), sort(Y, Y + n);
        int len1 = unique(X, X + n) - X;
        int len2 = unique(Y, Y + n) - Y;
        for(int i = 0; i < n; i++){
            A[i].x = lower_bound(X, X + len1, A[i].x) - X;
            A[i].y = lower_bound(Y, Y + len2, A[i].y) - Y;
        }
        sort(A, A + n);
        int num;
        int ans = 0;
        for(int i = 0; i < n; i++){
            if(A[i].y == 0) num = A[i].w;
            else num = A[i].w + query(1, 0, n, 0, A[i].y - 1);
            update(1, 0, n, A[i].y, num);
            ans = max(ans, num);
        }
        printf("%d\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Head_Hard/article/details/82084607
今日推荐