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. [题 ...
随机推荐
- 51nod 1686 第K大区间 二分瞎搞
题目: 定义一个区间的值为其众数出现的次数. 现给出n个数,求将所有区间的值排序后,第K大的值为多少. 题解: 答案明显单调,我们考虑二分答案. 转化为判定问题后我们需要观察到一个性质: 如果一个区间 ...
- SQL中replace函数
string sql1 = "select price from dbo.eazy_farm where REPLACE(title,' ','')='" + cainame + ...
- Python模块-logging模块(二)
logging模块记录日志有四个主要类:logger,handler,formatter,filter logger提供了应用程序可以直接使用的接口,每个程序在输出信息之前都要获得一个Logger h ...
- 与input有关的一些操作
单选 : 通过name指定为一组,只能选择一个 一组一个选项 <input type=" name="sex"/>男 <input type=&quo ...
- 可定制的分词库——Yaha(哑哈)分词
可定制的分词库——Yaha(哑哈)分词在线测试地址:http://yaha.v-find.com/ 部署于GAE yahademo.appspot.comYaha分词主要特点是把分词过程分成了4个阶段 ...
- JAVAWeb SSH框架 利用POI 导出EXCEL,弹出保存框
导入包这一些不多说,直接贴出关键代码,JSP只要点一个Action链接就行. poi包我是用:poi-3.11-20141221.jar 亲测有效: 效果: Action 类代码: private I ...
- MySql数据查询的逻辑蕴含条件问题
SQL语言中没有蕴含逻辑运算.但是,可以利用谓词演算将一个逻辑蕴含的谓词等价转换为:p->q ≡┐p∨q. 我们通过一个具体的题目来分析:(具体的表和数据详见文章:Mysql数据库中的EXIST ...
- SpringSecurity04 利用JPA和SpringSecurity实现前后端分离的认证和授权
1 环境搭建 1.1 环境说明 JDK:1.8 MAVEN:3.5 SpringBoot:2.0.4 SpringSecurity:5.0.7 IDEA:2017.02旗舰版 1.2 环境搭建 创建一 ...
- 9.利用msfvenom生成木马
这篇文章来介绍一下msf中一个生成木马的msfvenom模块. msfvenom命令行选项如下: 英文原版: 中文版: Options: -p, --payload <payload> 指 ...
- C#结构体指针的定义及使用详解(intptr的用法)
在解析C#结构体指针前,必须知道C#结构体是如何定义的.在c#中同样定义该结构体. C#结构体指针之C#结构体的定义: [StructLayout(LayoutKind.Sequential)] pu ...