题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447

YJJ's Salesman

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 919    Accepted Submission(s): 290

Problem Description

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) on the rectangle map and B (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) now (0≤x≤109,0≤y≤109), he will only forward to (x+1,y), (x,y+1) or (x+1,y+1).
On the rectangle map from (0,0) to (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 k on (xk,yk) (1≤xk≤109,1≤yk≤109), only the one who was from (xk−1,yk−1) to (xk,yk) will be able to earn vk 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 T (1≤T≤10),which is the number of test cases.
In each case, the first line of the input contains an integer N (1≤N≤105).The following N lines, the k-th line contains 3 integers, xk,yk,vk (0≤vk≤103), which indicate that there is a village on (xk,yk) and he can get vk 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
 
Source

题目大意:

有N个村庄 [ 横纵坐标为 x , y ( x , y >= 1 && x , y <=1e9 ) ] , 每到一个村庄 我们可以选择向上向右或者向右上方走,每个村庄有一个贡献值,只有当我是从左下方的村庄来的才可以得到当前村庄的贡献值。

解题思路:

离散化 +排序+树状数组维护区间最大值

坐标 x, y 的范围太大,而节点范围还可以,所以不妨离散化一波(听说实际测试数据范围没有到1e9,出题人就料定我们没有这个胆)。

一开始用传统 dp ,状态转移很好想,但很明显会超时,交了两波TLE果断自闭。

这道题是正确打开方式是线段树或者树状数组维护区间最大值,而这里的区间是 y 的区间。然后通过前一行的最优值去转移得到当前行的最优值,不过排序顺序需要注意,因为只有左下方的可以转移过来所以当 x 相同时,优先处理 y 大的。

AC code:

 #include <bits/stdc++.h>
#define ll long long int
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std; const int MAXN = 1e5+; struct date{
int x, y, v;
}node[MAXN]; int d[MAXN], dp[MAXN];
int t[MAXN<<];
int N; bool cmp1(struct date a, struct date b)
{
if(a.x == b.x) return a.y > b.y;
return a.x < b.x;
}
int lowbit(int x)
{
return x&(-x);
}
void add(int st, int val)
{
for(int i = st; i <= MAXN; i+=lowbit(i))
t[i] = max(t[i], val);
}
int query(int st)
{
int res = ;
for(int i = st; i > ; i-=lowbit(i))
res = max(res, t[i]);
return res;
} int main()
{
int T;
scanf("%d", &T);
while(T--){
scanf("%d", &N);
memset(t, , sizeof(t));
for(int i = ; i <= N; i++){
scanf("%d%d%d", &node[i].x, &node[i].y, &node[i].v);
d[i] = node[i].y;
}
sort(d+, d+N+);
int last = unique(d+, d++N)--d;
for(int i = ; i <= N; i++)
node[i].y = lower_bound(d+, d++last, node[i].y)-d;
sort(node+, node++N, cmp1);
for(int i = ; i <= N; i++)
{
int res = query(node[i].y-) + node[i].v;
add(node[i].y, res);
}
printf("%d\n", query(MAXN-));
}
return ;
}

2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】的更多相关文章

  1. 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...

  2. 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】

    Tree and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  3. HDU - 6440 Dream 2018中国大学生程序设计竞赛 - 网络选拔赛

    给定的\(p\)是素数,要求给定一个加法运算表和乘法运算表,使\((m+n)^p = m^p +n^p(0 \leq m,n < p)\). 因为给定的p是素数,根据费马小定理得 \((m+n) ...

  4. 2018中国大学生程序设计竞赛 - 网络选拔赛 Dream hdu6440 Dream 给出一个(流氓)构造法

    http://acm.hdu.edu.cn/showproblem.php?pid=6440 题意:让你重新定义任意一对数的乘法和加法结果(输出乘法口诀表和加法口诀表),使得m^p+n^p==(m+n ...

  5. hdu6444 2018中国大学生程序设计竞赛 - 网络选拔赛 1007 Neko's loop

    Neko's loop Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total S ...

  6. 2018中国大学生程序设计竞赛 - 网络选拔赛 Solution

    A - Buy and Resell 题意:给出n个交易点,每次能够选择买或者卖,求获得最大利润 思路:维护两个优先队列,一个是卖,一个是替换,当价格差相同时,优先替换,因为次数要最少 #includ ...

  7. 2018中国大学生程序设计竞赛 - 网络选拔赛 4 - Find Integer 【费马大定理+构造勾股数】

    Find Integer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  8. 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu 6440 Dream 模拟

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

  9. 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu6438 Buy and Resell 买入卖出问题 贪心

    Buy and Resell Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

随机推荐

  1. Linux私房菜阅读笔记

    在线man:http://www.linux.com/linux-man-pages 计算机硬件五大单元: 1.输入 2.输出 3.控制单元 4.算数逻辑单元 5.内存   CPU种类 1.RISC( ...

  2. JavaScript设计模式(二) - 单例模式

    什么是单例模式? 单例模式从字面上的理解是不困难的,js上就是指只有一个对象实例. 为什么需要单例模式? 我们可以将一些成员变量封装在一个单例对象中,每次访问这些变量都只能从这个单例对象进行访问,这样 ...

  3. Python的静态方法和类成员方法都可以被类或实例访问,两者概念不容易理清,但还是有区别的

    转:http://www.cnblogs.com/2gua/ Python的静态方法和类成员方法都可以被类或实例访问,两者概念不容易理清,但还是有区别的: 1)静态方法无需传入self参数,类成员方法 ...

  4. Ural 1260 Nudnik Photographer

    Problem Description If two people were born one after another with one second difference and one of ...

  5. JS中彻底删除json对象组成的数组中的元素

    只是分享一个小知识~ 在JS中,对于某个由json对象组成的数组,例如: var test = [{ "a": "1", "b": &quo ...

  6. ztree使用方法 python后台

    一.在提前加载js的地方写一段js,判断该页面是否需要添加ztree,我的项目所有提前加载的js都写在admin.js中 //增加ztree $(document).ready(function() ...

  7. jqGrid -treeGrid 按需加载

    Load Rows On Demand (AJAX) 参考:http://www.guriddo.net/demo/treegridjs/

  8. axios请求报Uncaught (in promise) Error: Request failed with status code 404

    使用axios处理请求时,出现的问题解决 当url是远程接口链接时,会报404的错误: Uncaught (in promise) Error: Request failed with status ...

  9. vue路由配置

    1.安装 npm install vue-router --save / cnpm install vue-router --save 2.引入并 Vue.use(VueRouter) (main.j ...

  10. lua_nginx_module用例

    content_by_lua server { listen ; server_name lua.luckybing.top; location / { default_type 'text/plai ...