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个点,问 ...
随机推荐
- 精读 SBAR SDN flow-Based monitoring and Application Recognition
目录 架构 测量模块 分类模块 具体实现 实验:最后接入巴塞罗那的校园网流量测试: SBAR: SDN flow-Based monitoring and Application Recognitio ...
- zookeeper启动时报错:Error contacting service. It is probably not running问题
查看zookeeper.out发现启动日志报错未找到java路径. 启动日志位于zookeeper-4.0.10/bin目录下 修改/etc/profile中环境变量得以解决.
- QueryRunner cannot be resolved to a type:关于包不能正常导入的问题
在操作一个功能模块的时候,出现一个问题: 我原则是按着项目指导一步一步走的,但却出现, QueryRunner cannot be resolved to a type,这个问题应该属于Xxx can ...
- Apache安装排错
今天安装一下Apache,发现报错,且在网上没有找到相关解决方法,所以记录一下 安装步骤:将下载好的apache包放置到要放置的目录中,最好是盘根目录下,然后命令行下进入到apache下面的bin目录 ...
- CUDA 版本,显卡驱动,Ubuntu版本,GCC版本之间的对应关系
- 今天在Qt子界面中的Button,转到槽转不过去,报错Qt The class containing 'Ui::MainWindow' could not be found in...
在网上查了原因,因为我在修改button的名字时,没选中button,选中了子界面对话框Dialog,然后修改了名字,又没有改回去,所以button转到槽报错. 参考网站: https://zhida ...
- K8s集群安装和检查(经验分享)
一.组件方式检查 1. Master节点: root>> kubectl get cs 2. Node 节点: 无 二.服务方式检查 1. Master 节点: root>> ...
- Hive(10)-文件存储格式
Hive支持的存储数据的格式主要有:TEXTFILE .SEQUENCEFILE.ORC.PARQUET 一. 列式存储和行式存储 左边为逻辑表,右边第一个为行式存储,第二个为列式存储 1. 行式存储 ...
- ruby Rspec+jenkins+allure持续集成
1.Allure2使用说明 2.ruby下载allure的gem gem install allure-rspec 3.修改源码 C:\Ruby23-x64\lib\ruby\gems\2.3.0\g ...
- Linux内核程序的编译:模块化编译
内核在编译的时候,可以支持单独模块化编译,只需要一个小小的Makefile即可搞定. 步骤如下: 1.在任意位置创建目录 vim Makefile 2.在Makefile中添加如下代码(我的kerne ...