hdu-6447 YJJ's Salesman(离散化+树状数组)

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

//题目大意:有一个n*n的矩阵,输入n行数据x,y,v;表示坐标,和该坐标的值;可以向右,向下,或向右下走一步,只有向右下走一步且该格子有一个值,则可取得该值,求所取得的值的和的最大值;

//思路:对(x,y)进行排序,若x相同,则y大的优先,否则x小的优先;

这样就可以从上往下,从左网右搜索,用树状数组维护最大值

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
struct Node
{
    int x,y,val;
    friend bool operator<(const Node&A,const Node&B)
    {
        if(A.x!=B.x)return A.x<B.x;
        else return A.y>=B.y;
    }
}node[100005];
ll tree[100005];
int n;
int lowbit(int k)
{
    return k&(-k);
}
void update(int k,ll num)
{
    while(k<=n)
    {
        tree[k]=max(tree[k],num);
        k+=lowbit(k);
    }
}
ll query(int k)
{
    ll ans=0;
    while(k>0)
    {
        ans=max(ans,tree[k]);
        k-=lowbit(k);
    }
    return ans;
}
int main()
{
    int t;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            memset(tree,0,sizeof(tree));
            scanf("%d",&n);
            int yy[100005];
            for(int i=1;i<=n;i++)
            {
                scanf("%d%d%d",&node[i].x,&node[i].y,&node[i].val);
                yy[i]=node[i].y;
            }
            sort(yy+1,yy+1+n);
            sort(node+1,node+1+n);
            //for(int i=1;i<=n;i++)cout<<node[i].x<<endl;
            //ll out=-1;
            for(int i=1;i<=n;i++)
            {
                int index=lower_bound(yy+1,yy+1+n,node[i].y)-yy;
                ll temp=node[i].val+query(index-1);
                //out=max(out,temp);
                update(index,temp);
            }
            printf("%lld\n",query(n));
        }
    }
    return 0;

猜你喜欢

转载自blog.csdn.net/qq_37378303/article/details/82501120
今日推荐