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 1257 余数之和 —— 数论分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1257 \( \sum\limits_{i=1}^{n}k\%i = \sum\limits_ ...
- Django_form补充
问题1: 注册页面输入为空,报错:keyError:找不到password def clean(self): print("---",self.cleaned_data) ...
- js基础之变量类型
1.NAN(Not a number) 不是一个数字 自身:console.log(NaN==NaN)和console.log(NaN===NaN)返回值都是false; 其他函数,isNaN()可用 ...
- Centos环境docker的正确安装及疑难杂症
根据官方文档:https://docs.docker.com/install/linux/docker-ce/centos/搭建docker 1.卸载docker旧版本: sudo yum remov ...
- mybatis 学习二 conf xml 配置信息
xml映射配置文件 这个xml文件主要包括一下节点信息 * properties 属性 * settings 设置 * typeAliases 类型命名 ...
- vs快捷键复制当前行
vs快捷键 1)如果你想复制一整行代码,只需将光标移至该行,再使用组合键“Ctrl+C”来完成复制操作,而无需选择整行.2)如果你想剪切一整行代码,只需将光标移至该行,再使用组合键“Ctrl+X”来完 ...
- centos6 启动流程
具体过程:1)加载BIOS的硬件信息,执行BIOS内置程序.2)读取MBR(Master Boot Record)中Boot Loader中的引导信息.3)加载内核Kernel boot到内存中.4) ...
- commons-configuration读取配置文件
关键工具类: import org.apache.commons.configuration.CompositeConfiguration; import org.apache.commons.con ...
- ViewPage+Fragment(仿微信切换带通知)
第一步 : 布局文件 activity_main.xml <?xml version="1.0" encoding="utf-8"?> <Li ...
- ASP.NET 调试出现<%@ Application Codebehind="Global.asax.cs" Inherits="XXX.XXX.Global" Language="C#" %>
ASP.NET 调试出现<%@ Application Codebehind="Global.asax.cs" Inherits="XXX.XXX.Global&q ...