hdu 6447 YJJ's Salesman(线段树,离散化优化dp) (2018CCPC 网络选拔赛 )

博客讲述yjj从A(0,0)走到B(1e9,1e9),中间有n个村庄,从村庄西北方向走向村庄可赚取v美元,求最高金额。思路是将村庄按x升序y降序排序,对y坐标离散化,用线段树记录k - 1个村庄在各y上最大金额,查询y在[1,yk - 1]间最大值并更新线段树后输出答案。

YJJ's Salesman

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1524    Accepted Submission(s): 546


 

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

 

题意:

yjj从A(0,0)走到B(1e9,1e9),中间有n个村庄,从村庄的西北方向走向村庄可以赚取v美元(如(x-1,y-1)-->(x,y))

求能获得的最高金额;

 

思路,将村庄按x升序y降序排序,对y坐标离散化,然后对于第k个村庄(xk,yk),用线段树先记录k-1个村庄是在各个y上能获得的最大金额(因为按x升序,只需考虑y),查询y在[1,yk-1]间的最大值maxz,加上vk,更新线段树。

最后将答案输出;

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<algorithm>
#include<iostream>
#include<set>
#include<map>
#include<queue>
#include<stack>
using namespace std;
const int N = 100005;
typedef long long ll;
const ll mod = 1000000007;
struct my
{
	int x,y,c;
	bool operator<(const my a)const
	{
		if(a.x==x)return a.y<y;
		return a.x>x;
	}
}a[N];
int ly[N],lc[N];
int ax[N],ay[N];
int q[N<<2];
void updata(int p,int l,int r,int x,int y)
{
	if(l==r)
	{
		q[p]=max(q[p],y);
		return;
	}
	int mid=(l+r)>>1;
	if(mid>=x)updata(p<<1,l,mid,x,y);
	else updata(p<<1|1,mid+1,r,x,y);
	q[p]=max(q[p<<1],q[p<<1|1]);
}
int query(int p,int l,int r,int x,int y)
{
	if(l==x&&r==y)return q[p];
	int mid=(l+r)>>1;
	if(mid>=y)return query(p<<1,l,mid,x,y);
	else if(mid<x)return query(p<<1|1,mid+1,r,x,y);
	else return max(query(p<<1,l,mid,x,mid),query(p<<1|1,mid+1,r,mid+1,y));
}
void init(int p,int l,int r)
{
	q[p]=0;
	if(l==r)return ;
	int mid=(l+r)>>1;
	init(p<<1,l,mid);
	init(p<<1|1,mid+1,r);
}
int main()
{
	int t,n;
	scanf("%d",&t);
	while(t--)
	{
	
		int x,y,c;
		int xlen=0,ylen=0;
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].c);
			//	ax[xlen++]=a[len].x;
				ay[ylen++]=a[i].y;
		}
	    sort(a,a+n);
		//sort(ax,ax+xlen);
	//	xlen=unique(ax,ax+xlen)-ax;
		sort(ay,ay+ylen);
		ylen=unique(ay,ay+ylen)-ay;
		for(int i=0;i<n;i++)
		{
			//a[i].x=lower_bound(ax,ax+xlen,a[i].x)-ax+1;
			a[i].y=lower_bound(ay,ay+ylen,a[i].y)-ay+1;
		}
		int u=0;
		int lz=0;
		while(u<n)
		{
			int maxz=0;
			if(a[u].y>1) maxz = query(1,1,ylen,1,a[u].y-1);
			updata(1,1,ylen,a[u].y,maxz+a[u].c);
			u++;
		}

		printf("%d\n",q[1]);
		init(1,1,ylen);
	}
	return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值