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更优. 那 ...
随机推荐
- [RN] 03 - Resource Collection & AWS Auth
那些资源 一.三个例子 iReading Bilibili-React-Native ZhiHuDaily-React-Native 二.IM 例子 1. React Native Socket.io ...
- Linux服务器部署 Elasticsearch 成功,本机却访问不了
Elasticsearch版本: elasticsearch- 服务器版本: CentOS release 6.8 (Final) 问题: Linux服务器上部署了 Elasticsearch 5.5 ...
- Java并发编程、内存模型与Volatile
http://www.importnew.com/24082.html volatile关键字 http://www.importnew.com/16142.html ConcurrentHash ...
- Weblogic12 集群部署
1. 集群配置规划 服务器名称 地址和端口 AdminServer 192.168.15.1:7001 ManagedServer1 192.168.15.1:8001 ManagedServer2 ...
- 关于C#事件的理解
一.一个不错的例子 class FileFFF { public delegate void FileWatchEventHandler(object sender,EventArgs args);/ ...
- head first python 支持网站(可下载所有的代码和示例)
http://examples.oreilly.com/0636920003434/
- Senium 简介
有时候我们在用 requests 抓取页面的时候,得到的结果可能和在浏览器中看到的不一样,在浏览器中可以看到正常显示的页面数据,但是使用 requests 得到的结果并没有.这是因为 requests ...
- Linux chmod和chown更改文件目录的所属者命令的用法
一.chown 命令 用途:更改文件的所有者或组.命令由单词change owner组合而成. 使用示例: 1,更改文件的所有者: chown jim program.c 文件 program.c 的 ...
- windows下nodejs与coffeeScript环境搭建
[本文档摘抄自网上资料] 安装NodeJS和CoffeeScript方法 首先安装Node(因为nodeJs是服务器端javascript运行环境),到http://nodejs.org/下载对应格式 ...
- iOS - UICollectionView 瀑布流 添加表头视图的坑
UICollectionView 瀑布流 添加表头视图的坑 首先是,需求加了个头视图在顶部,在collectionView中的头视图跟TableView的不一样,TableView的表头只要设置tab ...