hdu6447(离散化+线段树+dp)(01背包)

题目:

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 (10^9,10^9). 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≤10^9,0≤y≤10^9), he will only forward to (x+1,y), (x,y+1) or (x+1,y+1).
On the rectangle map from (0,0) to (10^9,10^9), 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≤10^9,1≤yk≤10^9), 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≤10^5).The following N lines, the k-th line contains 3 integers, xk,yk,vk (0≤vk≤10^3), 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.

题意:

给你一些城市坐标,yjj要从(0,0)到(10^9,10^9),而且不会往回走,一个城市不会走两次,所以只能沿着(x+1,y), (x,y+1) or (x+1,y+1)走,每个城市,他可以赚V元,但他在(x,y)城市想赚到钱的话只有他从(x-1,y-1)这个城市来才行,就是斜着走才能拿到下一个城市的钱,现在问你怎么走能拿到最多钱。

看一眼数据范围,不能直接存坐标,需要离散化,离散化之后可以进行dp:
dp[i][j] = max(dp[i-1][j-1]+dp[i][j] , max(dp[i-1][j] , dp[i][j-1]));
但是空间还是不够用,需要进一步优化,这个时候看了题解,明白了,通过类似于01背包的思想,把城市通过y从大到小,x从小到大的方式进行排序,再通过线段树来维护区间最大值。

实际上学习了一波离散化:

int n = 0;
int cnt = 1;
memset(numm,0,sizeof(numm));
memset(lsx,0,sizeof(lsx));
memset(lsy,0,sizeof(lsy));
scanf("%d",&n);
for(int i = 0;i < n;i++){
    scanf("%d %d %d",&numm[i].X,&numm[i].Y,&numm[i].V);
    lsx[cnt] = numm[i].X;
    lsy[cnt++] = numm[i].Y;
}
sort(lsx,lsx+cnt);
sort(lsy,lsy+cnt);
int m = 0;
int k = 0;
m = unique(lsx,lsx+cnt)-lsx;
k = unique(lsy,lsy+cnt)-lsy;
for(int i = 0;i < n;i++){
    numm[i].X = lower_bound(lsx,lsx+m,numm[i].X)-lsx;
    numm[i].Y = lower_bound(lsy,lsy+k,numm[i].Y)-lsy;
}

代码:

#include<bits/stdc++.h>
using namespace std;
int dp[100005];
struct num
{
    int X;
    int Y;
    int V;
}numm[100005];

int lsx[100005];
int lsy[100005];
bool cmp(num a,num b)
{
    if(a.X != b.X)
        return a.X < b.X;
    return a.Y > b.Y;
}



//tree
struct tre
{
    int l;
    int r;
    int sum;
}tree[100005<<2];
void up(int rt)
{
    tree[rt].sum = max(tree[rt<<1].sum,tree[rt<<1|1].sum);
}
void build(int l,int r,int rt)
{
    tree[rt].l = l;
    tree[rt].r = r;
    tree[rt].sum = 0;
    if(l == r)
        return;
    int mid = (l+r)/2;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
}
void update(int p,int v,int rt)
{
        int l = tree[rt].l;
        int r = tree[rt].r;
        int mid = (l+r)/2;
        if(l == r){
            tree[rt].sum = v;
            return;
        }
        if(p > mid){
            update(p,v,rt<<1|1);
        }
        else{
            update(p,v,rt<<1);
        }
        up(rt);
}
int query(int L,int R,int rt)
{
    int l = tree[rt].l;
    int r = tree[rt].r;
    int mid = (l+r)/2;
    if(L <= l && r <= R){
        return tree[rt].sum;
    }
    if(L > mid)
        return query(L,R,rt<<1|1);
      else 
    if(R <= mid)
        return query(L,R,rt<<1);
      return max(query(L,mid,rt<<1),query(mid+1,R,rt<<1|1)); 
}


int main()
{
    int t =0;
    scanf("%d",&t);
    while(t--){
        int n = 0;
        //int x = 0;
        //int y = 0;
        int cnt = 1;
        memset(dp,0,sizeof(dp));
        memset(numm,0,sizeof(numm));
        memset(lsx,0,sizeof(lsx));
        memset(lsy,0,sizeof(lsy));
        scanf("%d",&n);
        for(int i = 0;i < n;i++){
            scanf("%d %d %d",&numm[i].X,&numm[i].Y,&numm[i].V);
            lsx[cnt] = numm[i].X;
            lsy[cnt++] = numm[i].Y;
        }
        /*
        for(int i = 1;i <= xx;i++){
            for(int j = 1;j <= yy;j++){
                dp[i][j][0] += dp[i-1][j-1][0];
            }
        }
        */
        sort(lsx,lsx+cnt);
        sort(lsy,lsy+cnt);
        int m = 0;
        int k = 0;
        m = unique(lsx,lsx+cnt)-lsx;
        k = unique(lsy,lsy+cnt)-lsy;
        for(int i = 0;i < n;i++){
            numm[i].X = lower_bound(lsx,lsx+m,numm[i].X)-lsx;
            numm[i].Y = lower_bound(lsy,lsy+k,numm[i].Y)-lsy;
            //x = max(x,numm[i].X);
            //y = max(y,numm[i].Y);
        }
        //离散化 
        /*
        for(int i = 0;i < n;i++){
            cout << X[i] << " " << Y[i] << endl;
        }
        */

        sort(numm,numm+n,cmp);
        build(0,k,1);
        for(int i = 0;i < n;i++){
            int j;
            for(j = i;j < n;j++){
                if(numm[j].X != numm[i].X)
                    break;
                int temp = 0;
                temp = query(0,numm[j].Y-1,1)+numm[j].V;
                if(temp > dp[numm[j].Y]){
                    dp[numm[j].Y] = temp;
                    update(numm[j].Y,dp[numm[j].Y],1);
                }
            }
            i = j-1;
        }
        int ans = 0;
        for(int i = 0;i < k;i++){
            ans = max(ans,dp[i]);
        }
        printf("%d\n",ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值