YJJ's Salesman HDU - 6447 (dp+线段树优化)

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)(0,0) on the rectangle map and B (109,109)(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)(x,y) now (0≤x≤109,0≤y≤109)(0≤x≤109,0≤y≤109), he will only forward to (x+1,y)(x+1,y), (x,y+1)(x,y+1) or (x+1,y+1)(x+1,y+1). 
On the rectangle map from (0,0)(0,0) to (109,109)(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 kk on (xk,yk)(xk,yk) (1≤xk≤109,1≤yk≤109)(1≤xk≤109,1≤yk≤109), only the one who was from (xk−1,yk−1)(xk−1,yk−1) to (xk,yk)(xk,yk) will be able to earn vkvk 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 TT (1≤T≤10)(1≤T≤10),which is the number of test cases. 

In each case, the first line of the input contains an integer NN (1≤N≤105)(1≤N≤105).The following NN lines, the kk-th line contains 3 integers, xk,yk,vkxk,yk,vk (0≤vk≤103)(0≤vk≤103), which indicate that there is a village on (xk,yk)(xk,yk) and he can get vkvk 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

思路:首先可以联想到在矩阵里移动的dp,dp[i][j]=max(dp[i-1][j],dp[i][j-1],dp[i-1][j-1]+v[i][j]),首先离散化是必然的。

其次这样的二维数组是开不了的(因为1e5*1e5爆空间了),所以先考虑滚动一维,dp[j]表示第j列能得到的最大值,有一点可以肯定,在同一列上的点,只能选择一个进行交易。所以按横坐标顺序一次选择是最优策略,所以滚动掉i这一维。

接着,第i列的最大值,可能来自前i行的某一列,所以可以考虑用线段树维护前j-1列的最值。(据说不用线段树,直接做也可以水过去)。

离散化后对x按从小到大排序,y按从大到小排序(类似01背包的滚动数组原理,需要逆序求解).然后边更新,边查询即可。

代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <time.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+7;
#define Lson l,m,rt<<1
#define Rson m+1,r,rt<<1|1
struct Point
{
    int x,y,w;
}p[maxn];
bool cmp(Point a,Point b)
{
    if(a.x==b.x) return a.y>b.y;
    return a.x<b.x;
}
int n;
int hash_x[maxn],hash_y[maxn];
struct Tree
{
    int l,r,Max;
}tree[maxn<<2];
void Build(int l,int r,int rt)
{
    tree[rt].l=l,tree[rt].r=r;
    tree[rt].Max=0;
    if(l==r) return;
    int m=(l+r)>>1;
    Build(Lson);
    Build(Rson);
}
void push_up(int rt)
{
    tree[rt].Max=max(tree[rt<<1].Max,tree[rt<<1|1].Max);
}
void updata(int pos,int v,int l,int r,int rt)
{
    if(l==r)
    {
        tree[rt].Max=max(v,tree[rt].Max);
        return;
    }
    int m=(l+r)>>1;
    if(pos>m) updata(pos,v,Rson);
    else updata(pos,v,Lson);
    push_up(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        return tree[rt].Max;
    }
    int m=(l+r)>>1;
    int ans1=0,ans2=0;
    if(R>m) ans1=query(L,R,Rson);
    if(L<m) ans2=query(L,R,Lson);
    return max(ans1,ans2);
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
		freopen("in.txt","r",stdin);
		freopen("out.txt","w",stdout);
	#endif
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].w);
            hash_x[i]=p[i].x,hash_y[i]=p[i].y;
        }
        sort(hash_x+1,hash_x+n+1);
        sort(hash_y+1,hash_y+n+1);
        int n1=unique(hash_x+1,hash_x+1+n)-hash_x-1;
        int n2=unique(hash_y+1,hash_y+1+n)-hash_y-1;
        for(int i=1;i<=n;i++)
        {
            p[i].x=lower_bound(hash_x+1,hash_x+1+n1,p[i].x)-hash_x;
            p[i].y=lower_bound(hash_y+1,hash_y+1+n2,p[i].y)-hash_y;
        }
        sort(p+1,p+1+n,cmp);
        int ans=0;
        Build(1,n,1);
        for(int i=1;i<=n;i++)
        {
            int tmp=0;
            if(p[i].y==1) tmp=p[i].w;
            else tmp=query(1,p[i].y-1,1,n,1)+p[i].w;
            updata(p[i].y,tmp,1,n,1);
            ans=max(ans,tmp);
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40774175/article/details/82056351