2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】
题目传送门: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
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
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
题目大意:
有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 【离散化+树状数组维护区间最大值】的更多相关文章
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】
Tree and Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- HDU - 6440 Dream 2018中国大学生程序设计竞赛 - 网络选拔赛
给定的\(p\)是素数,要求给定一个加法运算表和乘法运算表,使\((m+n)^p = m^p +n^p(0 \leq m,n < p)\). 因为给定的p是素数,根据费马小定理得 \((m+n) ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 Dream hdu6440 Dream 给出一个(流氓)构造法
http://acm.hdu.edu.cn/showproblem.php?pid=6440 题意:让你重新定义任意一对数的乘法和加法结果(输出乘法口诀表和加法口诀表),使得m^p+n^p==(m+n ...
- 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 ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 Solution
A - Buy and Resell 题意:给出n个交易点,每次能够选择买或者卖,求获得最大利润 思路:维护两个优先队列,一个是卖,一个是替换,当价格差相同时,优先替换,因为次数要最少 #includ ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 4 - Find Integer 【费马大定理+构造勾股数】
Find Integer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu 6440 Dream 模拟
Dream Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu6438 Buy and Resell 买入卖出问题 贪心
Buy and Resell Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
随机推荐
- CSAPP阅读笔记-gcc常用参数初探-来自第三章3.2的笔记-P113
gcc是一种C编译器,这次我们根据书上的代码尝试着使用它. 使用之前,先补充前置知识.编译器将源代码转换为可执行代码的流程:首先,预处理器对源代码进行处理,将#define指定的宏进行替换,将#inc ...
- 【c#文档】在 C# 中,(int) ,Int32.Parse() 和 Convert.toInt32() 三种方法的区别
[c#文档]https://msdn.microsoft.com/zh-cn/library/system.convert.toint32.aspx 转载自:http://www.cnblogs.co ...
- Whu 1603——Minimum Sum——————【单个元素贡献、滑窗】
Problem 1603 - Minimum Sum Time Limit: 2000MS Memory Limit: 65536KB Total Submit: 623 Accepted: ...
- CF 304B——Calendar——————【年月日计算】
B - Calendar Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submi ...
- 深入理解JavaScript系列(16):闭包(Closures)
介绍 本章我们将介绍在JavaScript里大家经常来讨论的话题 —— 闭包(closure).闭包其实大家都已经谈烂了.尽管如此,这里还是要试着从理论角度来讨论下闭包,看看ECMAScript中的闭 ...
- JavaScript 监听回车事件
JS监听某个输入框 //回车事件绑定 $('#search_input').bind('keyup', function(event) { if (event.keyCode == "13& ...
- winform代码生成器(二)
代码下载 地址 http://pan.baidu.com/s/1nuZjyat 接着说 上文继续说,这次我们要生成主从表. 此方用到了第三方的 控件 DevExpress 的Gridview .大家可 ...
- python的元组及其书写规矩
1.元组 (1)元组看起来犹如列表,但使用圆括号而不是方括号来标识.定义元组后,就可以使用索引来访问其元素,就像访问列表元素一样. (2)元组的元素不可修改,但是可以赋值. 2.规矩 (1)缩进:建议 ...
- js简单时分秒倒计时
效果: javascript: <script type="text/javascript"> function countTime() { //获取当前时间 var ...
- mysql五补充:SQL逻辑查询语句执行顺序(待完善)
一.SELECT语句关键字的定义顺序(语法顺序) SELECT DISTINCT <select_list> FROM <left_table> <join_type&g ...