【HDU 6447】YJJ's Salesman 离散化+树状数组+DP

                                      YJJ's Salesman

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


 

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

 

代码:

#include<bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))

#define maxn 100010
int tree[maxn],dp[maxn],d[maxn];

struct ac
{
	int x,y,v;
}a[maxn];

bool cmp(ac x,ac y)
{
	if(x.y==y.y)
		return x.x<y.x;
	return x.y<y.y;
}

bool cmp1(ac x,ac y)
{
	if(x.x==y.x)
		return x.y<y.y;
	return x.x<y.x;
}

int lowbit(int i)
{
	return i&(-i);
}

int query(int i)
{
	int ans=0;
	while(i)
	{
		ans=max(ans,tree[i]);
		i-=lowbit(i);
	}
	return ans;
}

void update(int i,int x)
{
	while(i<maxn)
	{
		tree[i]=max(x,tree[i]);
		i+=lowbit(i);
	}
}

int main()
{
	int t,n,i,j;
	scanf("%d",&t);
	while(t--)
	{
		mem(tree,0);
		mem(a,0);
		mem(dp,0);
		mem(d,0);
		scanf("%d",&n);
		for(i=1;i<=n;i++)
			scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].v);
		sort(a+1,a+n+1,cmp);
		int tmp=a[1].y;
		int id=1;
		a[1].y=1;
		for(i=2;i<=n;i++)
		{
			if(a[i].y==tmp)
				a[i].y=id;
			else
			{
				tmp=a[i].y;
				id++;
				a[i].y=id;
			}
		}
		sort(a+1,a+n+1,cmp1);
		tmp=a[1].x;
		id=1;
		a[1].x=1;
		d[1]=1;
		for(i=2;i<=n;i++)
		{
			if(a[i].x==tmp)
			{
				a[i].x=id;
				d[id]++;
			}
			else
			{
				tmp=a[i].x;
				id++;
				a[i].x=id;
				d[id]++;
			}
		}
		int maxx=0;
		id=0;
		int l=1,r,x,y,v;
		for(i=1;i<=n;i++)
		{
			if(!d[i])
				continue;
				for(j=1;j<=d[i];j++)
			{
				int k=j+id;
				x=a[k].x;
				y=a[k].y;
				v=a[k].v;
				int ans1=query(y-1)+v;
				int ans2=query(y);
				dp[y]=max(ans1,ans2);
			}
			for(j=1;j<=d[i];j++)
			{
				int k=j+id;
				x=a[k].x;
				y=a[k].y;
				v=a[k].v;
				maxx=max(maxx,max(dp[y],query(y-1)));
				update(y,max(dp[y],query(y-1)));
			}
			id+=d[i];
		}
		printf("%d\n",maxx);
	}
	return 0;
}

 

 

 

 

 

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 18:02
好不容易拿到了字节Offer,鼠鼠做后端的,但家里人觉得可能被裁员不稳定,让鼠鼠去投国企,现在好纠结到底该咋选
文档传偷助手:该投就投吧,不过建议别放弃offer 拿到手里的才是最好的
投递字节跳动等公司9个岗位
点赞 评论 收藏
分享
头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务