无法拯救我的菜系列------hdu6447 YJJ's Salesman

YJJ’s Salesman
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1716 Accepted Submission(s): 635

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

可以推出动态规划方程
我们假设 dp[i] 为走到村庄 i 时能获得的最大的 v,则状态转移方程为:
dp[i]=max(dp[j]+v[i]),其中村庄 j 的坐标 (x,y) 满足 x≤x0−1 且 y≤y0−1
原本还想着用二分找最右边符合条件的坐标,没写出来,菜。。。。

转载:http://www.cnblogs.com/dilthey/p/9539618.html
那么,简单地说,对于每个村庄,要能 O(logn) 获得某区域内的最大值,同时也要能 O(logn) 的某区域内的最大值,自然而然想到树状数组……

我们离散化纵坐标,并且从小到大枚举横坐标,用树状数组维护纵坐标为 [1,y] 区域内最大的dp[i],

1、计算到某个横坐标值上某一村庄 dp[i],假设其纵坐标为 y,查询 [1,y−1] 区域内最大值;

2、每次计算完某个横坐标值的一竖条上的所有村庄的 dp[i],将这一竖条上所有的 dp[i] 全部拿去更新树状数组。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 5;

#define lowbit(x) x&(-x)

typedef struct Node{
    int x,y,z;
    friend bool operator < (const Node &p,const Node &q)
    {
        if(p.x == q.x){
            return p.y < q.y;
        }
        else{
            return p.x < q.x;
        }
    }
    void input()
    {
        scanf("%d %d %d",&x,&y,&z);
    }
}Node;

Node node[N];
int dp[N];
int a[N];
int c[N];
int sum;

void update(int i,int val)
{
    while(i <= sum)
    {
        a[i] = max(a[i],val);
        i += lowbit(i);
    }
}

int query(int i)
{
    int res = 0;
    while(i > 0){
        res = max(res,a[i]);
        i -= lowbit(i);
    }
    return res;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        memset(a,0,sizeof(a));
        scanf("%d",&n);
        for(int i = 0;i < n;++i)
            node[i].input();
        sum = 0;
        for(int i = 0;i < n;++i)
        {
            c[sum++] = node[i].y;
        }
        //去重,将纵坐标压缩,缩小树状数组的空间
        sort(c,c + sum);
        sum = unique(c,c + sum) - c;
        for(int i = 0;i < n;++i)
        {
            node[i].y = lower_bound(c,c + sum,node[i].y) - c + 1;
        }
        //将坐标离散化
        sort(node,node + n);
        //dp数组初始化
        dp[0] = 0;
        for(int i = 0;i < n;++i)
        {
            dp[i] = node[i].z;
        }
        int ans = 0;
        int pos = 0;
        for(int i = 0;i < n;++i)
        {
            //到下一个x坐标,将前一个x坐标上的点全部更新
            while(pos < i && node[pos].x < node[i].x)
            {
                update(node[pos].y,dp[pos]);
                pos++;
            }
            dp[i] = query(node[i].y - 1) + node[i].z;
            //cout << query(node[i].y - 1) <<  endl;
            ans = max(ans,dp[i]);
        }
        cout << ans << endl;
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值