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 4261: 建设游乐场 费用流
题目 现在有一大块土地,可以看成N*M的方格.在这块土地上,有些格子内是崎岖的山地,无法建造任何东西:其他格子都是平原.现在打算在这块土地上建设一个游乐园.游乐园由若干条闭合的过山车轨道组成,每个平原 ...
- bzoj 4407 于神之怒加强版 —— 反演+筛积性函数
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4407 推导如这里:https://www.cnblogs.com/clrs97/p/5191 ...
- Django_form补充
问题1: 注册页面输入为空,报错:keyError:找不到password def clean(self): print("---",self.cleaned_data) ...
- Azure 用户自定义路由 (User Defined Route)
在公有云环境中,用户创建了一个Vnet,添加了若干个网段后,这几个网段是全联通的状态. 如果希望在Vnet中添加一些功能性的设备,比如防火墙.IPS.负载均衡设备等,就需要进行用户自定义路由的配置. ...
- hdu 1074 状态压缩
http://acm.hdu.edu.cn/showproblem.php?pid=1074 我们可以断定状态的终止态一定是n个数全部选完的情况,那么它的前一个状态是什么呢,一定是剔除任一门课程后的n ...
- 杂项:grunt-tmod
ylbtech-杂项:grunt-tmod 前端模板预编译工具 tmodjs 的grunt自动化插件. 1.返回顶部 1. grunt-tmod 前端模板预编译工具 tmodjs 的grunt自动化插 ...
- angular-cli.json配置参数解析,常用命令解析
1.angular-cli.json配置参数解析 { "project": { "name": "ng-admin", //项目名称 &qu ...
- c语言基础 c和指针
句子 c规定数组名代表数组首元素的地址 如果&a 则代表整个数组 没有内存哪来的指针 数据类型的本质:固定大小内存的别名 变量的本质:(一段连续)内存空间的别名,内存空间的标号 指针是一种数据 ...
- 关于web中注册倒数的问题(亲测)
<title></title> <script type="text/javascript"> var leftSecond ...
- SpringMVC RESTful中文乱码
开发中常遇到各种中文乱码很少心烦,这里总结了各种中文乱码https://www.cnblogs.com/lwx521/p/9856186.html 下面以SpringMVC遇到的中文乱码为例详解 首先 ...