题干:

YJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination. 
One day, he is going to travel from city A to southeastern city B. Let us assume that A is (0,0)(0,0) on the rectangle map and B (109,109)(109,109). YJJ is so busy so he never turn back or go twice the same way, he will only move to east, south or southeast, which means, if YJJ is at (x,y)(x,y) now (0≤x≤109,0≤y≤109)(0≤x≤109,0≤y≤109), he will only forward to (x+1,y)(x+1,y), (x,y+1)(x,y+1) or (x+1,y+1)(x+1,y+1). 
On the rectangle map from (0,0)(0,0) to (109,109)(109,109), there are several villages scattering on the map. Villagers will do business deals with salesmen from northwestern, but not northern or western. In mathematical language, this means when there is a village kk on (xk,yk)(xk,yk) (1≤xk≤109,1≤yk≤109)(1≤xk≤109,1≤yk≤109), only the one who was from (xk−1,yk−1)(xk−1,yk−1) to (xk,yk)(xk,yk) will be able to earn vkvk dollars.(YJJ may get different number of dollars from different village.) 
YJJ has no time to plan the path, can you help him to find maximum of dollars YJJ can get.

Input

The first line of the input contains an integer TT (1≤T≤10)(1≤T≤10),which is the number of test cases.

In each case, the first line of the input contains an integer NN (1≤N≤105)(1≤N≤105).The following NN lines, the kk-th line contains 3 integers, xk,yk,vkxk,yk,vk (0≤vk≤103)(0≤vk≤103), which indicate that there is a village on (xk,yk)(xk,yk) and he can get vkvk dollars in that village. 
The positions of each village is distinct.

Output

The maximum of dollars YJJ can get.

Sample Input

1
3
1 1 1
1 2 2
3 3 1

Sample Output

3

题目大意:

商人从(0,0)走到(1e9,1e9)途中有若干村庄,商人可以向(x,y+1)(x+1, y)(x+1, y+1)三个方向前进,当向(x+1,y+1)方向前进时可以获得村庄(x+1, y+1)的利润,问商人途中最多可以挣多少钱

解题报告:

商人到达点(x,y)的最大利润一定是从(0,0)到(x-1,y-1)这个矩形转移而来的,但是如果单纯的用二维来dp会超空间,可以考虑dp的顺序而降低dp维度,dp顺序从上到下,从右到左,这样dp[i] = max(dp[k]) + val[i][j] 1 < K < i这样就降低了一维,用树状数组来处理前缀最大值。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
struct Point {int x,y,v;bool operator <(const Point & b) const {if(x != b.x) return x < b.x;else return y > b.y;}
} p[MAX];
int b[MAX],c[MAX],ans,n;
int get(int x) {return lower_bound(b+1,b+n+1,x) - b;
}
int query(int x) {int res = 0;while(x > 0) {res = max(res,c[x]);x -= x&-x;}return res;
}
void update(int x,int val) {while(x < MAX) {c[x] = max(c[x],val);x += x&-x;}
}
int main()
{int t;cin>>t;while(t--) {cin>>n;ans=0;for(int i = 1; i<=n; i++) scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].v),b[i] = p[i].y,c[i] = 0;sort(b+1,b+n+1);sort(p+1,p+n+1);for(int i = 1; i<=n; i++) {int pos = get(p[i].y);int val = query(pos-1) + p[i].v;ans = max(ans,val);update(pos,val);}printf("%d\n",ans);}return 0 ;
}

【HDU - 6447】YJJ's Salesman(降维dp,树状数组优化dp)相关推荐

  1. E. Pencils and Boxes (树状数组优化dp)

    传送门 1.题意 给出n个数,要求给这些数分组,每组不少于k个. 每组的数之间的差不能大于d.是否能够分组? 2.dp分析 先排序. f[i]f[i]f[i]表示[1~i]能够分组成功. 答案就是f[ ...

  2. HDU 6447 YJJ's Salesman(树状数组优化DP + 离散化)

    HDU 6447 YJJ's Salesman 题目 给一个二维数组,从(0,0)走到(1e9, 1e9).每次只能走右,下,右下,三个方向.其中只有通过右下走到特定给出的点(村庄)时才会获得分值.问 ...

  3. HDU 4455 Substrings(dp+树状数组优化)

    题意: 给定一个序列ai,个数为n.再给出一系列w: 对于每个w,求序列中,所有长度为w的连续子串中的权值和,子串权值为子串中不同数的个数 解析: 一直想不出来怎么转移,看了网络上面的题解才明白怎么做 ...

  4. Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)

    题意: 线段树做法 分析: 因为每次都是在当前位置的前缀区间查询最大值,所以可以直接用树状数组优化.比线段树快了12ms~ 代码: #include<cstdio> #include< ...

  5. [SCOI2014]方伯伯的玉米田 //二维树状数组优化DP

    题目: 方伯伯在自己的农田边散步,他突然发现田里的一排玉米非常的不美.这排玉米一共有N株,它们的高度参差不齐.方伯伯认为单调不下降序列很美,所以他决定先把一些玉米拔高,再把破坏美感的玉米拔除掉,使得剩 ...

  6. hdu 4991(树状数组优化dp)

    Ordered Subsequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  7. hdu 5542(树状数组优化dp)

    题意: 求n个数中长度为m的上升子序列的个数 解题思路:dp[i][j]表示第i个数长度为j的上升序列的个数.dp[i][j] = sum{dp[k][j-1] | a[k] < a[i]},这 ...

  8. bzoj 1264: [AHOI2006]基因匹配Match (树状数组优化dp)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1264 思路: n大小为20000*5,而一般的dp求最长公共子序列复杂度是 n*n的,所以我 ...

  9. 小魂和他的数列(dp+树状数组优化)

    链接:https://ac.nowcoder.com/acm/contest/3566/C 来源:牛客网 Sometimes, even if you know how something's goi ...

最新文章

  1. Go 学习笔记(6)— 变量定义、变量声明、变量作用域
  2. MyBatis和SpringMVC集成事务在Junit测试下有效但是在实际项目无效的问题
  3. Gym - 100625E Encoded Coordinates 矩阵快速幂
  4. linux日志中显示skipping,linux推荐使用logrotate自动管理日志文件
  5. C++多线程并发中线程管理
  6. 创建基于密码的加密密钥
  7. C++ operator操作符重载(++,--,-,+,())
  8. zlib1.2.5的编译
  9. aspnetcore源码学习(一)
  10. win7中能对窗口的排列方法是_win7系统窗口排列方式怎么修改?修改窗口排列方式方法...
  11. DICOM worklist入门一
  12. html form表格采购单,【HTML】--- 列表、表格、form表单标签
  13. 未来世界的幸存者 -- 阮一峰
  14. 杭电数字电路课程设计——出租车计费器
  15. 创客集结号:3D打印如何与中小学教育有机结合?
  16. Android桌面插件系列
  17. 战神引擎去右上角信息
  18. 乐高机器人走进图书馆活动方案_乐高机器人制作~~乐高大派对
  19. 关于c#的书籍下载网站和地址
  20. 数据结构(c语言版严蔚敏_吴伟民)读书笔记 第一章

热门文章

  1. jQuery 实现一个简单的信息反馈或者信息收集的页面
  2. 用批处理实现文本文件中指定字符串的替换 zz
  3. 已触发了一个断点 vs_VSCode源码分析-断点调试
  4. mysql 字典索引_【大白话mysql】你真的了解 mysql 索引吗?
  5. android休眠后恢复线程,关于Android系统休眠后,线程的执行情况
  6. java单车吧_暑假一个月,撸个共享单车后台
  7. datatables分页下一页不能点击_干货,删不掉Word文末最后一页?学会5个方法,再也不愁啦...
  8. 属于db模式缺点的是什么_详解 Seata Golang 客户端 AT 模式及其使用
  9. RxJava之PublishSubject、BehaviorSubject、ReplaySubject和AsyncSubject
  10. javapanel根据内部组件_[译]避免在unmounted组件上调用setState