题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447

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
2018中国大学生程序设计竞赛 - 网络选拔赛

题意:

从 $(0,0)$ 往 $(10^9,10^9)$ 走,每次只能从 $\left( {x,y} \right)$ 走到 $\left( {x + 1,y} \right)$ 或者 $\left( {x,y + 1} \right)$ 或者 $\left( {x + 1,y + 1} \right)$,不能折返,不能走重复的路,

在地图上分布着一些村庄,给出村庄的坐标 $\left( {x,y} \right)$ 和 $v$ 值,当且仅当你从 $\left( {x - 1,y - 1} \right)$ 走到村庄时,你才可以获得 $v$,求最大能获得多少 $v$。

题解:

显然,假设我走到了某一个村庄 $\left( {x_0,y_0} \right)$,那么只有在 $x \ge x_0 + 1$ 且 $y \ge y_0 + 1$ 范围内的村庄,能使得我的 $v$ 值进一步增加,

换句话说,我走到了某一个村庄 $\left( {x_0,y_0} \right)$,我上一个走到的“有意义的”村庄必然是在 $x \le x_0 - 1$ 且 $y \le y_0 - 1$ 的范围内的,

那么我就要在 $x \le x_0 - 1$ 且 $y \le y_0 - 1$ 的范围内找到某个村庄,我走到该村庄时,我获得的 $v$ 值时最大的,

故,我们假设 $dp\left[ i \right]$ 为走到村庄 $i$ 时能获得的最大的 $v$,则状态转移方程为:

$dp\left[ i \right] = \max \left( {dp\left[ j \right] + v\left[ i \right]} \right)$,其中村庄 $j$ 的坐标 $\left( {x,y} \right)$ 满足 $x \le x_0 - 1$ 且 $y \le y_0 - 1$

那么,简单地说,对于每个村庄,要能 $O\left( {\log n} \right)$ 获得某区域内的最大值,同时也要能 $O\left( {\log n} \right)$ 的某区域内的最大值,自然而然想到树状数组……

我们离散化纵坐标,并且从小到大枚举横坐标,用树状数组维护纵坐标为 $\left[ 1,y \right]$ 区域内最大的dp[i],

1、计算到某个横坐标值上某一村庄 $dp\left[ i \right]$,假设其纵坐标为 $y$,查询 $\left[ 1,y - 1 \right]$ 区域内最大值;

2、每次计算完某个横坐标值的一竖条上的所有村庄的 $dp\left[ i \right]$,将这一竖条上所有的 $dp\left[ i \right]$ 全部拿去更新树状数组。

AC代码:

#include<bits/stdc++.h>
using namespace std; const int maxn=1e5+; struct Node{
int x,y,v;
}node[maxn];
bool cmp(Node a,Node b)
{
if(a.x==b.x) return a.y<b.y;
else return a.x<b.x;
} int n;
int dp[maxn];
vector<int> X[maxn]; vector<int> Y;
inline getid(int y){return lower_bound(Y.begin(),Y.end(),y)-Y.begin()+;}
inline getval(int id){return Y.at(id-);} struct _BIT
{
int N,C[maxn];
int lowbit(int x){return x&(-x);}
void init(int n)//初始化共有n个点
{
N=n;
for(int i=;i<=N;i++) C[i]=;
}
void update(int pos,int val)
{
while(pos<=N)
{
C[pos]=max(C[pos],val);
pos+=lowbit(pos);
}
}
int askmax(int pos)
{
int ret=;
while(pos>)
{
ret=max(ret,C[pos]);
pos-=lowbit(pos);
}
return ret;
}
}BIT; int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n;
Y.clear();
for(int i=;i<n;i++)
{
scanf("%d%d%d",&node[i].x,&node[i].y,&node[i].v);
if(node[i].x== || node[i].y==) node[i].v=;
Y.push_back(node[i].y);
} sort(Y.begin(),Y.end());
Y.erase(unique(Y.begin(),Y.end()),Y.end()); sort(node,node+n,cmp); int tot=;
for(int i=;i<n;i++)
{
node[i].y = getid(node[i].y);
if(i== || node[i].x>node[i-].x)
{
tot++;
X[tot].clear();
}
X[tot].push_back(i);
} BIT.init(n);
int ans=;
for(int i=;i<=tot;i++)
{
int now;
for(int j=;j<X[i].size();j++)
{
now=X[i][j];
dp[now]=BIT.askmax(node[now].y-)+node[now].v;
ans=max(ans,dp[now]);
}
for(int j=;j<X[i].size();j++)
{
now=X[i][j];
BIT.update(node[now].y,dp[now]);
}
} cout<<ans<<endl;
}
}

附一个bin神的代码

#include <bits/stdc++.h>
using namespace std; struct Node {
int x, y, v;
void input() {
scanf("%d%d%d", &x ,&y, &v);
}
}node[];
bool cmp(Node a, Node b) {
return a.x < b.x;
} int a[];
int tot; int c[];
int lowbit(int x) {
return x&(-x);
} void update(int i, int val) {
while (i <= tot) {
c[i] = max(c[i], val);
i += lowbit(i);
}
}
int query(int i) {
int res = ;
while (i > ) {
res = max(res, c[i]);
i -= lowbit(i);
}
return res;
} int dp[]; int main() {
int T;
int n;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i = ; i < n; i++)
node[i].input();
tot = ;
for (int i = ; i < n; i++) {
a[tot++] = node[i].y;
}
sort(a, a+tot);
tot = unique(a, a+tot) - a;
for (int i = ; i < n; i++) {
node[i].y = lower_bound(a, a+tot, node[i].y) - a + ;
}
sort(node, node+n, cmp);
for (int i = ; i < n; i++) dp[i] = node[i].v;
for (int i = ; i <= tot; i++) c[i] = ;
int pos = ;
int ans = ;
for (int i = ; i < n; i++) {
while (pos < i && node[pos].x < node[i].x) {
update(node[pos].y, dp[pos]);
pos++;
}
dp[i] = query(node[i].y - ) + node[i].v;
ans = max(ans, dp[i]);
}
printf("%d\n",ans);
}
return ;
}

非常简洁,空间消耗很少,%%%

HDU 6447 - YJJ's Salesman - [树状数组优化DP][2018CCPC网络选拔赛第10题]的更多相关文章

  1. HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)

    题目链接  2017 CCPC Harbin Problem K 题意  给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...

  2. Codeforces 946G Almost Increasing Array (树状数组优化DP)

    题目链接   Educational Codeforces Round 39 Problem G 题意  给定一个序列,求把他变成Almost Increasing Array需要改变的最小元素个数. ...

  3. LUOGU P2344 奶牛抗议 (树状数组优化dp)

    传送门 解题思路 树状数组优化dp,f[i]表示前i个奶牛的分组的个数,那么很容易得出$f[i]=\sum\limits_{1\leq j\leq i}f[j-1]*(sum[i]\ge sum[j- ...

  4. 【题解】Music Festival(树状数组优化dp)

    [题解]Music Festival(树状数组优化dp) Gym - 101908F 题意:有\(n\)种节目,每种节目有起始时间和结束时间和权值.同一时刻只能看一个节目(边界不算),在所有种类都看过 ...

  5. 【题解】ARC101F Robots and Exits(DP转格路+树状数组优化DP)

    [题解]ARC101F Robots and Exits(DP转格路+树状数组优化DP) 先删去所有只能进入一个洞的机器人,这对答案没有贡献 考虑一个机器人只能进入两个洞,且真正的限制条件是操作的前缀 ...

  6. Codeforces 909C Python Indentation:树状数组优化dp

    题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...

  7. BZOJ3594: [Scoi2014]方伯伯的玉米田【二维树状数组优化DP】

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

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

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

  9. BZOJ 3594: [Scoi2014]方伯伯的玉米田 (二维树状数组优化DP)

    分析 首先每次增加的区间一定是[i,n][i,n][i,n]的形式.因为如果选择[i,j](j<n)[i,j](j<n)[i,j](j<n)肯定不如把后面的全部一起加111更优. 那 ...

随机推荐

  1. CentOS-6.4 安装 PHP Memcached 扩展

    1.获取安装文件包 [root@phpdragon home]# wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmem ...

  2. 给button添加边框和圆角

    button是我们经常用到的控件,我把它的属性罗列一下: UIButton *Button = [[UIButton alloc] initWithFrame:CGRectMake(, , , )]; ...

  3. Oracle之表空间基于时间点的恢复

    记一次优化过程中:一次误操作,在不影响其他表空间的情况下:采用表空间基于时间点的恢复(TSPITR)方法恢复数据的过程. 1.TSPITR恢复原理    TSPITR目前最方便的方法是使用RMAN进行 ...

  4. Apache ActiveMQ Fileserver远程代码执行漏洞

    扫端口的时候遇到8161端口,输入admin/admin,成功登陆,之前就看到过相关文章,PUT了一句话上去,但是没有什么效果,于是本地搭建了一个环境,记录一下测试过程. 环境搭建: ActiveMQ ...

  5. C#编码习惯谈

    1.  避免将多个类放在一个文件里面.2.  一个文件应该只有一个命名空间,避免将多个命名空间放在同一个文件里面.3.  一个文件最好不要超过500行的代码(不包括机器产生的代码).4.  一个方法的 ...

  6. DrawCall 优化 .

    unity3D 对于移动平台的支持无可厚非,但是也有时候用Unity3D 开发出来的应用.游戏在移动终端上的运行有着明显的效率问题,比如卡.画质等各种问题.自己在做游戏开发的时候偶有所得.对于主要影响 ...

  7. 【delphi】delphi的TAdoQuery读取Excel数据

    1. 连接 需要设置TAdoQuery的连接串Connection,将其指向excel文件: 'Provider=Microsoft.ACE.OLEDB.12.0;Data Source=' + ex ...

  8. 《转载》IntelliJ 2016.2 IDEA 中进行 Java Web 项目开发配置

    本文转载自 https://segmentfault.com/a/1190000007088964 1. 新建一个 Web Application 项目 打开 IntelliJ,选择新建项目: 左边栏 ...

  9. linux-find【递归搜索文件名】

    1 命令格式: $ find <指定目录> <指定条件> <指定动作>   最常见示例:查找文件名(忽略大小写) $find . -iname sqlquery.j ...

  10. Matlab练习——矩阵和数组的操作

    题目来自:<战胜MATLAB必做练习50道> 题目有更改,改成了我想写的样子. 1. 创建一个3×3矩阵,并将其扩充为4×5矩阵 clear; clc; mat1 = ones(,) ma ...