HDU 6447

YJJ's Salesman

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


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 (0x109,0y109), 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) (1xk109,1yk109), only the one who was from (xk1,yk1) 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 (1T10),which is the number of test cases.

In each case, the first line of the input contains an integer N (1N105).The following N lines, the k-th line contains 3 integers, xk,yk,vk (0vk103), 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
 

 

 

int a[N],x[N];
int t,n,ret,ans;
void  update(int i,int num){
    while(i<=n){
        a[i]=max(a[i],num);
        i+=lowbit(i);
    }
}
int query(int n)
{
    int ans=0;
    while(n>0)
    {
        ans=max(ans,a[n]);
        n-=lowbit(n);
    }
    return ans;
}
struct M{
    int x,y,ux,uy,w;
}mm[N];
bool cmp3(M a,M b){
    if(a.y!=b.y)  return a.y<b.y;
    return a.x>b.x;
    //按列的话要按上式排序,x的排序不能错
}
int  main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        mem(a,0);
        gep(i,1,n){
            scanf("%d%d%d",&mm[i].x,&mm[i].y,&mm[i].w);
            x[i]=mm[i].x;
        }


//都要排序 sort(x
+1,x+1+n);//为了lower_bound sort(mm+1,mm+1+n,cmp3);//为了下面的更新 ret=0,ans=0; gep(i,1,n){//按列 int k=lower_bound(x+1,x+1+n,mm[i].x)-x;//1~N ret=query(k-1)+mm[i].w;//1~k-1 的最大值+ mm[i].w update(k,ret); ans=max(ans,ret); } printf("%d\n",ans); } return 0; }

 

posted on 2018-08-27 17:26  cltt  阅读(91)  评论(0编辑  收藏  举报

导航