HDU 6447 - YJJ's Salesman - [树状数组优化DP][2018CCPC网络选拔赛第10题]
题目链接: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题]的更多相关文章
- HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)
题目链接 2017 CCPC Harbin Problem K 题意 给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...
- Codeforces 946G Almost Increasing Array (树状数组优化DP)
题目链接 Educational Codeforces Round 39 Problem G 题意 给定一个序列,求把他变成Almost Increasing Array需要改变的最小元素个数. ...
- LUOGU P2344 奶牛抗议 (树状数组优化dp)
传送门 解题思路 树状数组优化dp,f[i]表示前i个奶牛的分组的个数,那么很容易得出$f[i]=\sum\limits_{1\leq j\leq i}f[j-1]*(sum[i]\ge sum[j- ...
- 【题解】Music Festival(树状数组优化dp)
[题解]Music Festival(树状数组优化dp) Gym - 101908F 题意:有\(n\)种节目,每种节目有起始时间和结束时间和权值.同一时刻只能看一个节目(边界不算),在所有种类都看过 ...
- 【题解】ARC101F Robots and Exits(DP转格路+树状数组优化DP)
[题解]ARC101F Robots and Exits(DP转格路+树状数组优化DP) 先删去所有只能进入一个洞的机器人,这对答案没有贡献 考虑一个机器人只能进入两个洞,且真正的限制条件是操作的前缀 ...
- Codeforces 909C Python Indentation:树状数组优化dp
题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...
- BZOJ3594: [Scoi2014]方伯伯的玉米田【二维树状数组优化DP】
Description 方伯伯在自己的农田边散步,他突然发现田里的一排玉米非常的不美. 这排玉米一共有N株,它们的高度参差不齐. 方伯伯认为单调不下降序列很美,所以他决定先把一些玉米拔高,再把破坏美感 ...
- Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)
题意: 线段树做法 分析: 因为每次都是在当前位置的前缀区间查询最大值,所以可以直接用树状数组优化.比线段树快了12ms~ 代码: #include<cstdio> #include< ...
- BZOJ 3594: [Scoi2014]方伯伯的玉米田 (二维树状数组优化DP)
分析 首先每次增加的区间一定是[i,n][i,n][i,n]的形式.因为如果选择[i,j](j<n)[i,j](j<n)[i,j](j<n)肯定不如把后面的全部一起加111更优. 那 ...
随机推荐
- Eclipse------新建文件时没有JSP File解决方法
1.为没有web选项的eclipse添加web and JavaEE插件 .在Eclipse中菜单help选项中选择install new software选项 .在work with 栏中输入 Ju ...
- web.xml 整合 SpringMVC
啦啦啦 <context-param> <param-name>defaultHtmlEscape</param-name> <param-value> ...
- headless webkit(无界面浏览器、爬虫)
phantomjs / puppeteer https://github.com/amir20/phantomjs-node https://github.com/GoogleChrome/puppe ...
- Splash go() 方法
go()方法用来请求某个链接,而且它可以模拟 GET 和 POST 请求,同时支持传入请求头.表单等数据 function main(splash) ok, reason = splash:go(&q ...
- C语言中一个字符数组里面的所有元素变成一个字符串
#include <string.h> int main() // 这里为了方便直接用main函数 { char array[] = { 'h', 'e', 'l', 'l', ' ...
- PowerShell的初步学习
今天要重新学习一钟语法,由于工作中项目的需要,不得不说学习新的语言是必不可少的. Windows PowerShell 是一种命令行外科程序和脚本环境,使命令行用户和脚本编写者可以利 ...
- codeforces水题100道 第七题 Codeforces Round #270 A. Design Tutorial: Learn from Math (math)
题目链接:http://www.codeforces.com/problemset/problem/472/A题意:给你一个数n,将n表示为两个合数(即非素数)的和.C++代码: #include & ...
- 《Lua程序设计》第7章 迭代器与泛型for 学习笔记
本章将介绍如何编写适用于泛型for的迭代其(Iterator).7.1 迭代器与closurehttp://www.cnblogs.com/moonlightpoet/p/5685275.html 7 ...
- Linux配置示例:配置java环境变量
1.修改/etc/profile文件 如果你的计算机仅仅作为开发使用时推荐使用这种方法,因为所有用户的shell都有权使用这些环境变量,可能会给系统带来安全性问题. (1)用文本编辑器打开/etc ...
- 《转载》IntelliJ 2016.2 IDEA 中进行 Java Web 项目开发配置
本文转载自 https://segmentfault.com/a/1190000007088964 1. 新建一个 Web Application 项目 打开 IntelliJ,选择新建项目: 左边栏 ...