题意:

二维平面上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 + 离散)的更多相关文章

  1. 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 ...

  2. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  3. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  4. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  5. hdu 4622 Reincarnation trie树+树状数组/dp

    题意:给你一个字符串和m个询问,问你l,r这个区间内出现过多少字串. 连接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 网上也有用后缀数组搞得. 思路 ...

  6. HDU 5862 Counting Intersections (树状数组)

    Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...

  7. 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 ...

  8. HDU 1394 Minimum Inversion Number (树状数组求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...

  9. HDU 5877 Weak Pair(树状数组)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5877 [题目大意] 给出一棵带权有根树,询问有几对存在祖先关系的点对满足权值相乘小于等于k. [题 ...

随机推荐

  1. 2017-2018-1 20179203 《Linux内核原理与分析》第七周作业及第三周测试总结

    攥写人:李鹏举 学号:20179203 ( 原创作品转载请注明出处) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.study.163.com/course/US ...

  2. bzoj 2002: 弹飞绵羊 Link-Cut-Tree

    题目: Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置, ...

  3. 采用MySQL_upgrade升级授权表方式升级

    1.7.1 采用MySQL_upgrade升级授权表方式升级(1) 这种升级方式比较省事,通过MySQL_upgrade命令即可完成.下面来演示一下整个升级过程. 1)修改my.cnf配置文件,因为M ...

  4. Poj 1458 Common Subsequence(LCS)

    一.Description A subsequence of a given sequence is the given sequence with some elements (possible n ...

  5. poj 1658 Eva's Problem(水题)

    一.Description Eva的家庭作业里有很多数列填空练习.填空练习的要求是:已知数列的前四项,填出第五项.因为已经知道这些数列只可能是等差或等比数列,她决定写一个程序来完成这些练习. Inpu ...

  6. Netty组件

    一.Channel.EventLoop 和ChannelFuture 这些类合在一起,可以被认为是Netty 网络抽象的代表: Channel—Socket: EventLoop—控制流.多线程处理. ...

  7. SpringBoot系列(1)

    简介:用来简化新Spring应用的初始搭建以及开发过程:该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. 特点:1. 创建独立的Spring应用程序2. 嵌入的Tomcat, ...

  8. centos6 搭建IPSEC

    http://www.maxwhale.com/how-to-install-l2tp-vpn-on-centos/ http://blog.earth-works.com/2013/02/22/ho ...

  9. [poj1088]滑雪(二维最长下降子序列)

    解题关键:记忆化搜索 #include<cstdio> #include<cstring> #include<algorithm> #include<cstd ...

  10. html span

    提示:请使用 <span> 来组合行内元素,以便通过样式来格式化它们.注释:span 没有固定的格式表现.当对它应用样式时,它才会产生视觉上的变化.以上是w3上的标准解释,另外span是个 ...