hdu6447
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
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.
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.
3
1 1 1
1 2 2
3 3 1
#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的更多相关文章
- hdu6447 YJJ's Salesman
这个题意和数据范围一看就是离散化之后树状数组优化DP.给的"从左下方走上去才能拿到收益"的性质其实可以当成"必须从横纵坐标严格比某个点小的地方转移过来".1A了 ...
- 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) ...
- HDU6447(离散化扫描线+树状数组)
一眼看过去就x排序扫描一下,y是1e9的离散化一下,每层用树状数组维护一下,然后像dp倒着循环似的树状数组就用y倒着插就可行了. 类似题目练习:BZOJ4653.BZOJ1218 #pragma co ...
- HDU6447 YJJ's Salesman-2018CCPC网络赛-线段树求区间最值+离散化+dp
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 原题目描述在最下面. 1e5个点,问 ...
随机推荐
- js实现限制上传文件大小
<html> <head> <script type="text/javascript"> var isIE = /msie/i.test(na ...
- cocos2d-x 3.x 物理碰撞机制
近期又弄了物理引擎.写一下吧,以下有在其它博客学习到的知识.加上自己的理解,总结下. cocos2d-x 3.X 中全新的封装的物理引擎给了开发人员最大的便捷,你不用再繁琐与各种物理引擎的细节,全然的 ...
- 搭建springboot项目
1.搭建环境windows10+jdk1.8+eclipse4.8+maven 2.为了学习微服务架构学习搭建基础项目 3.分为两种搭建方式为maven项目和单独建立springboot项目(ecli ...
- Java并发编程(十一)常用工具
Java为开发提供了很多有用的工具类,这些工具类可以帮助我们更加高效的编写并发程序,本篇我们将介绍这些实用工具的用法. ThreadLocal ThreadLocal类用于解决多线程共享一个变量的问题 ...
- mysql事件关闭解决办法
Mysql 事件event_scheduler是OFF 开启 Event Scheduler,以下4种方式等效 SET GLOBAL event_scheduler = ON; SET @@globa ...
- 一次JVM内存调优过程
项目中,有个同事写的JOB,使用到查询数据库大量历史协议数据(大概300W左右),由于对存放数据的list或map没有做“用完即时声明释放”. 导致此Jar部署在windows service后,进程 ...
- jquery购物车添加功能
<html> <head> <meta charset="UTF-8"> <title></title> <scr ...
- 修复支付宝后台报错session IP change to many
在项目的web.xml 中添加: <init-param> <param-name>sessionStatEnable</param-name> <param ...
- 简述对Vuex的理解
1.什么是Vuex: Vuex是一个专为Vue.js应用程序开发的状态管理模式. 2.使用Vuex的原因: 当我们遇到多个组件共享状 ...
- Vue基础学习(纯属个人学习的笔记,慢慢新增)
1.在html文件中,声明了template对象,那么在 data对象中的v-html和v-text的绑定数据是不起作用的 2.v-的几个常用绑定 v-html和v-text:引用的conten ...