YJJ's Salesman

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

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
Source
 
数据范围太大,一开始要对数据进行离散化
一开始没做过离散化的题  ,就是把几个有大小顺序的数(很大)用一些小的数来记录排序
比如1,10000,10000000000排序和离散化后1,2,3排序是没啥区别的
接下来用线段树维护区间的最大值
是类似于01背包的求出最大值
具体看代码吧
#include<bits/stdc++.h>
#define lson l,m,k<<1
#define rson m+1,r,k<<1|1
using namespace std;
#define maxn 200010
int tree[maxn<<];
void pushup(int k)
{
tree[k]=max(tree[k<<],tree[k<<|]);
}
void build(int l,int r,int k)
{
if(l==r)
{
tree[k]=;
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
pushup(k);
}
int query_getmax(int x,int y,int l,int r,int k)//这里是求出最大值
{
if(x<=l&&r<=y)
return tree[k];
int m=(l+r)>>;
int ans=;
if(x<=m) ans=max(ans,query_getmax(x,y,lson));
if(y>m) ans=max(ans,query_getmax(x,y,rson));
return ans;
}
void update(int p,int c,int l,int r,int k)
{
if(l==r)
{
tree[k]=c;
return;
}
int m=(l+r)>>;
if(p<=m) update(p,c,lson);
else update(p,c,rson);
pushup(k);
}
struct point{
int x,y,w;
int ha_x,ha_y;//记录离散后的坐标
}a[];
bool cmp1(point a,point b)
{
return a.x<b.x;
}
bool cmp2(point a,point b)
{
return a.y<b.y;
}
bool cmp3(point a,point b)
{
if(a.x==b.x) return a.y>b.y;
return a.x<b.x;
}
int main()
{
int n,m;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int ha=;
build(,n,);
for(int i=;i<=n;i++)
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
sort(a+,a++n,cmp1);
int haxx=;
for(int i=;i<=n;i++)//对x坐标得点进行离散化
{
a[i].ha_x=haxx;
if(a[i+].x!=a[i].x)haxx++;
}
int hayy=;
sort(a+,a++n,cmp2);
for(int i=;i<=n;i++)//对y坐标进行离散化
{
a[i].ha_y=hayy;
if(a[i+].y!=a[i].y)hayy++;
}
sort(a+,a++n,cmp3);
haxx--,hayy--;
for(int i=;i<=n;i++)//跟一维01背包的更新可像
{
if(a[i].ha_y==) update(a[i].ha_y,a[i].w,,n,);//到了最下面,在1的线段树出更新
else
{
int t=query_getmax(,a[i].ha_y-,,n,)+a[i].w;//从1到a[i].ha_y 这个区间求出最大值
update(a[i].ha_y,t,,n,);//把这个点的最优值储存在a[i].ha_y上
}
}
cout<<query_getmax(,n,,n,)<<endl;
}
return ;
}
 

hdu6447的更多相关文章

  1. hdu6447 YJJ's Salesman

    这个题意和数据范围一看就是离散化之后树状数组优化DP.给的"从左下方走上去才能拿到收益"的性质其实可以当成"必须从横纵坐标严格比某个点小的地方转移过来".1A了 ...

  2. HDU6447 网络赛 YJJ's Salesman(DP + 线段树)题解

    思路:若用dp[i][j]表示走到(i,j)的最大值,那么dp[i][j] = max(dp[i - 1][j],dp[i][j - 1],dp[i - 1][j - 1] + v),显然O(n^2) ...

  3. HDU6447(离散化扫描线+树状数组)

    一眼看过去就x排序扫描一下,y是1e9的离散化一下,每层用树状数组维护一下,然后像dp倒着循环似的树状数组就用y倒着插就可行了. 类似题目练习:BZOJ4653.BZOJ1218 #pragma co ...

  4. HDU6447 YJJ's Salesman-2018CCPC网络赛-线段树求区间最值+离散化+dp

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门  原题目描述在最下面.  1e5个点,问 ...

随机推荐

  1. 用JavaScript中lodash编写双色球

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. SQL Server 数据库空间使用情况

    GO /****** Object: StoredProcedure [dbo].[SpaceUsed] Script Date: 2017-12-01 11:15:11 ******/ SET AN ...

  3. iOS中Block循环引用的问题

    说到循环引用问题,想必大家都碰到过吧,比如在使用Block的时候,使用__weakSelf来代替self解决等,但是对于这个,还是有不少可以探索的点,下面我就来说下,希望对大家有所帮助. 是否所有的B ...

  4. Ubuntu18.04安装mysql及相关配置

    step 1: sudo apt-get update step 2: sudo apt-get install mysql-server step3: 查看mysql服务端是否开启 systemct ...

  5. MySQL----MySQL数据库入门----第一章 数据库入门

    第一章 数据库入门 1.1 数据库基础知识 1.1.1 数据库概述 数据不仅包括普通意义上的数字,还包括文字.图像.声音等.也就是说,凡是在计算机中用来描述事物的记录都可称作数据. 数据库的基本特点: ...

  6. python面试题之基础

    一.基础语法 1. 输入与输出 1.1 代码中要修改不可变数据会出现什么问题? 抛出什么异常? (2018-3-29-lxy) 代码不会正常运行,抛出 TypeError 异常. 1.2a=1,b=2 ...

  7. 【六】tf和cgi进行联合试验,完成日志服务器

    [任务6]tf和cgi进行联合试验,完成日志服务器 [任务6]tf和cgi进行联合试验,完成日志服务器 改装gen-cpp目录下client.cpp文件 启动Nginx服务和gen-cpp目录下编译后 ...

  8. 调试日志——基于stm32的智能声光报警器(三)

    智能声光报警器基本功能调试完成. 1.通过拨码开关来设置LED闪烁的频率. 2.关门时喇叭不想,灯熄灭. 3.旁路模式时,灯处于闪烁状态,此时关门灯扔闪烁. 关于此次代码我觉得还是有可以优化的地方,电 ...

  9. linux打patch简单示例

    在项目中,有些模块是开源的,没有源码或者不能改动源码,想要修复.优化里面的Bug,这时就需要用到patch了. 1.    生成patch 制作补丁有两种法法,diff和quilt. 1.1   di ...

  10. Zeta--S3 Linux优化/缩短开机时间

    U-Boot1)axp20_set_ldo3实现里面把两个__msdelay(200);去掉,节省400ms2)sys_config.fex把下面的used设置为0,不使用开机指示灯闪烁,可以省掉35 ...