HDU 6447 YJJ’s Salesman (树状数组 + DP + 离散)
题意:
二维平面上N个点,从(0,0)出发到(1e9,1e9),每次只能往右,上,右上三个方向移动,
该N个点只有从它的左下方格点可达,此时可获得收益。求该过程最大收益。
分析:我们很容易就可以想到用DP,假设这个位置是相对上一个位置的方向而来,但是复杂度达到N^2 ,这样是不行的;
我们可以利用坐标的信息,将所有的点离散化后,以x优先按小到大排序,按y按大到小排序,这时维护一个DP(i) ,表示第I列的最值。
j=0→i−1j=0→i−1
dp[i]←max(dp[i[,dp[j]+val)dp[i]←max(dp[i[,dp[j]+val)
因为x已经按从小到大排好序了,y也是从大到小更新的,故保证了可达性。
对于每次更新,可以用线段树或者树状数组维护最大值,此时算法复杂度O(NlogN)
#include<bits/stdc++.h>
using namespace std;
int n,hashx[],hashy[],dp[],tree[];
struct no
{
int x,y,w;
}a[];
bool cmp(no a, no b)
{
if(a.x==b.x)
return a.y>b.y;
return a.x<b.x; }
//离散化
void init( )
{
for(int i= ; i<=n ; i++)
{
hashx[i] = a[i].x;
hashy[i] = a[i].y;
}
sort(hashx+,hashx++n);
sort(hashy+,hashy++n);
int cntx = unique(hashx+,hashx++n)-hashx;
int cnty = unique(hashy+,hashy++n)-hashy;
for(int i= ; i<=n ; i++)
{
a[i].x = lower_bound(hashx+,hashx++cntx,a[i].x)-hashx;
a[i].y = lower_bound(hashy+,hashy++cnty,a[i].y)-hashy;
} }
int lowbit(int x)
{
return x&(-x);
}
void update(int pos)
{
while(pos <= n)
{
tree[pos] = dp[pos];
for(int i=;i<lowbit(pos);i<<=)
tree[pos] = max(tree[pos],tree[pos-i]);
pos += lowbit(pos);
}
} int query(int l, int r)
{
int ans = ;
while(r>=l)
{
ans = max(ans,dp[r]);
if(l==r) break;
for(--r;r-l>=lowbit(r);r-=lowbit(r))
ans = max(ans,tree[r]);
}
return ans;
}
int main( )
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
for(int i= ; i<=n ; i++)
{
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
}
sort(a+,a++n,cmp);
init( );
memset(dp,,sizeof(dp));
memset(tree,,sizeof(tree));
for(int i= ; i<=n ; i++)
{
dp[a[i].y]=max(dp[a[i].y],query(,a[i].y-)+a[i].w);
update(a[i].y); }
int ans = ;
for(int i=;i<=n;++i)
ans = max(ans,dp[i]);
printf("%d\n",ans);
}
return ;
}
HDU 6447 YJJ’s Salesman (树状数组 + DP + 离散)的更多相关文章
- HDU 6447 - YJJ's Salesman - [树状数组优化DP][2018CCPC网络选拔赛第10题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 Problem DescriptionYJJ is a salesman who has tra ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- hdu 4622 Reincarnation trie树+树状数组/dp
题意:给你一个字符串和m个询问,问你l,r这个区间内出现过多少字串. 连接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 网上也有用后缀数组搞得. 思路 ...
- HDU 5862 Counting Intersections (树状数组)
Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...
- hdu 5592 ZYB's Game 树状数组
ZYB's Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=55 ...
- HDU 1394 Minimum Inversion Number (树状数组求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...
- HDU 5877 Weak Pair(树状数组)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5877 [题目大意] 给出一棵带权有根树,询问有几对存在祖先关系的点对满足权值相乘小于等于k. [题 ...
随机推荐
- bzoj 3727: Final Zadanie 思维题
题目: Description 吉丽YY了一道神题,题面是这样的: "一棵n个点的树,每条边长度为1,第i个结点居住着a[i]个人.假设在i结点举行会议,所有人都从原住址沿着最短路径来到i结 ...
- CQOI2018做题记录
T1.破解D-H协议 传送门 这个题就是BSGS的板子题-- 然后这里补充一点嘛,就是第二重循环的枚举范围.我们是在枚举\(a^{tm-y}\),把tm换成i,这个的最大值就是\(i - (m - 1 ...
- POJ2080:Calendar(计算日期)
Calendar Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12842 Accepted: 4641 Descrip ...
- HUD1455:Sticks
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- ng2 样式控制之style绑定和class绑定
- python 基础 字典 小例子
统计单词次数 作为字典存储 cotent = "who have an apple apple is free free is money you know" result = { ...
- angularJS中自定义指令
学习了angularJS一周,但是大部分时间被自定义指令占用了.博主表示自学互联网好心塞的,发现问题的视觉很狭窄,这比解决问题要更难.这篇文章首先介绍了自定义,然后介绍了在使用自定义指令遇到的问题. ...
- Java探索之旅(18)——多线程(2)
1 线程协调 目的对各线程进行控制,保证各自执行的任务有条不紊且有序并行计算.尤其是在共享资源或者数据情况下. 1.1 易变volatile cache技术虽然提高了访问数据的效率,但是有可能导致主存 ...
- 6.7 使用IDEA导入工程
打开IDEA->File->new -> Project from existing ..->选择你的工程,导入: 请注意,在130或者40上面的项目并不是最新的,sunny也 ...
- C# 5.0中新增特性
C# 5.0随着VisualStudio 2012一起正式发布了,让我们来看看C#5.0中增加了哪些功能. 1. 异步编程 在.Net 4.5中,通过async和await两个关键字,引入了一种新的基 ...