HDU6447 YJJ's Salesman

YJJ's Salesman

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


 

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

 

Source

2018中国大学生程序设计竞赛 - 网络选拔赛

主要倒着更新,没想到... ...

#include <bits/stdc++.h>
#define mid ((tr[d].l + tr[d].r)>>1)
#define lc (d << 1)
#define rc (d << 1|1)
using namespace std;
const int maxn = 1e5+5;
struct Tr{
    int l,r,mx;
}tr[maxn*4];
struct Node{
    int x,y,w;
    friend operator < (Node a,Node b){ //倒着更新,类似背包,否则会对后面操作产生影响
        if(a.x!=b.x)
            return a.x<b.x;
        else
            return a.y>b.y;
    }
}node[maxn];
int T,n;
int hash_x[maxn],hash_y[maxn];
int n1,n2;

void build(int d,int l,int r){
    tr[d].l = l,tr[d].r = r,tr[d].mx = 0;
    if (tr[d].l == tr[d].r){
        return;
    }
    build(lc,l,mid); 
    build(rc,mid+1,r);
}

int query(int d,int l,int r){
    if (tr[d].l == l && tr[d].r == r){
        return tr[d].mx;
    }
    if (mid >= r) return query(lc,l,r);
    else if (mid < l) return (rc,l,r);
    else return max(query(lc,l,mid),query(rc,mid+1,r));
}

void update(int d,int pos,int w){
    if (tr[d].l == tr[d].r && tr[d].r == pos) {
        tr[d].mx = max(w,tr[d].mx);
        return;
    }
    if (mid >= pos) update(lc,pos,w);
    else update(rc,pos,w);
    tr[d].mx = max(tr[lc].mx,tr[rc].mx);
}

int main(){
    scanf("%d",&T);
    while(T--){

        scanf("%d",&n);
        for (int i = 0;i < n;++i){
            scanf("%d%d%d",&node[i].x,&node[i].y,&node[i].w);
            hash_x[i] = node[i].x,hash_y[i] = node[i].y;
        }
        sort(hash_x,hash_x+n);
        sort(hash_y,hash_y+n);
        n2 = unique(hash_y,hash_y+n) - hash_y;
        for (int i = 0;i < n;++i){
            node[i].y = lower_bound(hash_y,hash_y+n2,node[i].y) - hash_y;
        }
        sort(node,node+n);
        build(1,0,n);
        int ans = 0;
        for (int i = 0;i < n;++i){
            int tmp;
            if (node[i].y == 0) tmp = node[i].w;
            else tmp = query(1,0,node[i].y-1) + node[i].w;
            update(1,node[i].y,tmp);
            ans = max(ans,tmp);
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41548233/article/details/82178000