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个点,问 ...
随机推荐
- HDU 1116 Play on Words(欧拉回路+并查集)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1116 Play on Words Time Limit: 10000/5000 MS (Java/Ot ...
- 框架 Hibernate
Hibernate 在test01右键新建其他找到hibernate文件夹下的Hibernate Configuration File(cfg.xml) <?xml version=" ...
- Linux TCP server 只能接受一个 TCP 连接
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <str ...
- nginx编译问题:make[1]: *** [/usr/local/pcre//Makefile] Error 127
解决方法: 是由于nginx高版本的需要使用pcre原文件路径. 解压pcre-7.9.tar.gz 例如解压后位置在 /home/wang/pcre-7.9位置 使用nginx配置的时候 ./con ...
- ios 开发UI篇— UIToolbar
前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIToolbar : UIView <UIBarPositioning& ...
- bfs,队列
bfs bfs=队列 队列的操作 头文件 #include<deque> 声明方法: 1.普通声明 queue<int>q; 2.结构体 struct node { int x ...
- centos6 命令界面切换到图形界面
要进入图形界面,首先要安装.所以应该先执行 yum groupinstall "X Window System" -y yum groupinstall "Desktop ...
- cookie、localstorage、sessionstorage区别
localstorage sessionsorage cookie 大小 浏览器不能保存超过300个cookie,单个服务器不能超过20个,每个cookie不能超过4k 可以达到5M 可以达到5M ...
- 课时54.audio标签(掌握)
1.什么是audio标签? 播放音频 格式: <audio src=""> </audio> 也是由于同样的适配问题,所以出现了第二种格式 <audi ...
- ILOVEYOU代码
代码确实很简单...我是初学者,练手的. /* 文件名: Love.c 描 述: 打印字母和图形 */ #include<stdio.h> #include<windows.h> ...