YJJ's Salesman(线段树+dp)

YJJ's Salesman

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


 

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中国大学生程序设计竞赛 - 网络选拔赛

Recommend

chendu   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 

 

题意:有n个点,每个点有一个价值,必须从(x-1,y-1)走到(x,y)才能获得(x,y)点的价值,当走到最后能获得的最大价值是多少?

题解:首先对于1e9的x坐标和y坐标无法开数组,于是先对x轴和y轴进行离散,很容易可以想到对于一个点的最大值就是找到y和x都小于这个点的最大值再加上这个点的价值,于是用线段树为何0--p[i].y-1行的最大值,直接进行转移即可。

#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int maxn=1e6+7;

struct node
{
    int x,y,w;
    bool operator <(const node &a)const
    {
        return (x==a.x)?y>a.y:x<a.x;
    }
}p[maxn];

int tree[maxn],n;
void build(int rt=1,int l=0,int r=n-1)
{
    tree[rt]=0;
    if(l==r) return;
    int m=l+r>>1;
    build(rt<<1,l,m);
    build(rt<<1|1,m+1,r);
}
void update(int pos,int w,int l=0,int r=n-1,int rt=1)
{
    if(l==r)
    {
        tree[rt]=max(tree[rt],w);
        return;
    }
    int m=l+r>>1;
    if(pos<=m) update(pos,w,l,m,rt<<1);
    else update(pos,w,m+1,r,rt<<1|1);
    tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}
int query(int L,int R,int l=0,int r=n-1,int rt=1)
{
    if(l>=L && r<=R)
        return tree[rt];
    int MAX=0;
    int m=l+r>>1;
    if(L<=m) MAX=max(MAX,query(L,R,l,m,rt<<1));
    if(R>m) MAX=max(MAX,query(L,R,m+1,r,rt<<1|1));
    return MAX;
}

int hax[maxn],hay[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].w);
            hax[i]=p[i].x;hay[i]=p[i].y;
        }
        sort(hax,hax+n);sort(hay,hay+n);
        int n1=unique(hax,hax+n)-hax;
        int n2=unique(hay,hay+n)-hay;
        for(int i=0;i<n;i++)
        {
            p[i].x=lower_bound(hax,hax+n1,p[i].x)-hax;
            p[i].y=lower_bound(hay,hay+n2,p[i].y)-hay;
        }
        sort(p,p+n);
        build();

        int ans=0;
        for(int i=0;i<n;i++)
        {
            int tmp;
            if(!p[i].y) tmp=p[i].w;
            else tmp=query(0,p[i].y-1)+p[i].w;
            update(p[i].y,tmp);
            ans=max(ans,tmp);
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sudu6666/article/details/82260377