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
分析:
dp[i][j]是走到(i,j)的最大金钱。dp[i][j]=max(dp[i][j],dp[k][l]+v[i][j]) (k<i&&l<j)
x,y,太大只能离散化,二维的dp开不了并且扫描二维容易超时,只能线段树优化查找最大值。
比较难理解的部分在题解中说明了
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#define maxn 100010
using namespace std;
long long num[maxn];
long long dp[maxn];
struct point{
long long x;
long long y;
long long z;
}p[maxn];
bool cmp1(point a,point b){
if (a.x<b.x)return 1;
else if(a.x==b.x){
if(a.y<b.y)return 1;
return 0;
}
return 0;
}
bool cmp2(point a,point b){
if (a.y<b.y)return 1;
else if (a.y==b.y){
if (a.x<b.x)return 1;
return 0;
}
return 0;
}
struct xtree{
long long l,r;
long long maxx;
long long mid(){
return (l+r)/2;
}
}tree[4*maxn];
void pushup(long long id){
tree[id].maxx=max(tree[id*2].maxx,tree[id*2+1].maxx);
}
void build(long long id,long long l,long long r){
tree[id].l=l;
tree[id].r=r;
if (l==r){
tree[id].maxx=0;
return ;
}
long long mid=tree[id].mid();
build(id*2,l,mid);
build(id*2+1,mid+1,r);
pushup(id);
}
void update(long long id,long long pos,long long val){
if (tree[id].l==tree[id].r){
tree[id].maxx=max(val,tree[id].maxx);///对于每一行求最大值
return ;
}
long long mid=tree[id].mid();
if (pos<=mid){
update(id*2,pos,val);
}
else {
update(id*2+1,pos,val);
}
pushup(id);
}
long long query(long long id,long long l,long long r){///对于矩形求最大值
if (tree[id].l>=l&&tree[id].r<=r){
return tree[id].maxx;
}
long long mid=tree[id].mid();
if (r<=mid){
return query(id*2,l,r);
}
else if (l>mid){
return query(id*2+1,l,r);
}
else {
return max(query(id*2,l,mid),query(id*2+1,mid+1,r));
}
}
int main(){
long long t;
long long n;
long long i,j;
scanf("%lld",&t);
while (t--){
scanf("%lld",&n);
queue<long long>qq;
for (i=1;i<=n;i++)scanf("%lld%lld%lld",&p[i].x,&p[i].y,&p[i].z);
sort (p+1,p+n+1,cmp2);
num[p[1].y]=1;
for (i=2;i<=n;i++){
if (p[i].y!=p[i-1].y){
num[p[i].y]=num[p[i-1].y]+1;
}
}
build(1,1,num[p[n].y]);
sort (p+1,p+n+1,cmp1);
long long maxx=0;
p[0].x=0;
p[0].y=0;
p[0].z=0;
long long temp=0;
for (i=1;i<=n;i++){
if (p[i].x!=p[i-1].x){
while (!qq.empty()){///将不是一个列(x=p[i].x)的进行更新
long long fuck=qq.front();
qq.pop();
update(1,num[p[fuck].y],dp[fuck]);
}
if (num[p[i].y]!=1){
dp[i]=p[i].z+query(1,1,num[p[i].y]-1);
}
else {
dp[i]=p[i].z;
}
qq.push(i);
}
else {
if (num[p[i].y]!=1){///延迟同一列的更新,先放到队列储存
dp[i]=p[i].z+query(1,1,num[p[i].y]-1);
}
else {
dp[i]=p[i].z;
}
qq.push(i);
}
if (dp[i]>maxx)maxx=dp[i];
}
printf("%lld\n",maxx);
}
}