@2018中国大学生程序设计竞赛 - 网络选拔赛: 1010: YJJ's Salesman(树状数组)

版权声明:岂曰无衣,与子同袍 https://blog.csdn.net/sizaif/article/details/82054704

 

YJJ's Salesman

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


 

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

 

就不吐槽 这场  CCPC网络赛了,

[题意]

1e9*1e9 的图 从 (0,0)开始 只能向下 和向左走, 图中有 m 个点, 每个点有个权值k,  每个取得话, 必须有个限制 ,必须从 (i-1,j-1)过

来取,   也就是说   每行 每列   只能取一个,

[思路]

输入n 在 1e5内,  图在 1e9内 ,  所以需要离散化 坐标,   对 x 排序,  对y 离散化,

然后  按照 x 从小到大,  y 从 右到左 排序,

然后 对 y轴 数组数组维护 前面得最大值. 这样得话 ,  每次 得到得最大值  加上 自己 本身 就是  走到 当前这个点

取到得最大值和.  大概这么个图

维护得时候 维护一个最大值 就是答案.

代码:

#include <bits/stdc++.h>
#include <stdio.h>

typedef long long ll;

const int maxn = 1e5+10;
const int inf = 0x3f3f3f3f;
using namespace std;

int n;
int tree[maxn],a[maxn],ans;
struct node
{
	int x,y,val;
	int id;
}p[maxn];


void add(int k,int num)
{
	while(k<=maxn)
	{
		tree[k] = max(num,tree[k]);
		ans = max(ans,tree[k]);
		k += k&(-k);
	}
}
int Qmax(int k)
{
	int res  =0 ;
	while(k)
	{
		res = max(tree[k],res);
		k -=k&(-k);
	}
	return res;
}


int cmp(node a,node b)
{
	return (a.x<b.x) || (a.x==b.x&&a.y>b.y);
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		memset(tree,0,sizeof(tree));
		memset(p,0,sizeof(p));
		memset(a,0,sizeof(a));
		ans = 0;
		scanf("%d",&n);
		for(int i = 1 ; i <= n; i++)
		{
			scanf("%d %d %d",&p[i].x,&p[i].y,&p[i].val);
			a[i] = p[i].y;
		}
		sort(a+1,a+n+1);
		int size=unique(a+1,a+n+1)-(a+1);// 去重求大小
		for(int i=1;i<=n;i++)
		{
			p[i].y = lower_bound(a+1,a+size+1,p[i].y)-(a+1) +1;
		}
		sort(p+1,p+n+1,cmp);
		for(int i=1;i<=n;i++)
		{
			ll val = Qmax(p[i].y-1)+p[i].val;
			add(p[i].y,val);
		}
		printf("%d\n",ans);
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/sizaif/article/details/82054704
今日推荐