YJJ's Salesman
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1258 Accepted Submission(s): 445
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.
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.
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
数据范围太大,一开始要对数据进行离散化
一开始没做过离散化的题 ,就是把几个有大小顺序的数(很大)用一些小的数来记录排序
比如1,10000,10000000000排序和离散化后1,2,3排序是没啥区别的
接下来用线段树维护区间的最大值
是类似于01背包的求出最大值
具体看代码吧
#include<bits/stdc++.h> #define lson l,m,k<<1 #define rson m+1,r,k<<1|1 using namespace std; #define maxn 200010 int tree[maxn<<2]; void pushup(int k) { tree[k]=max(tree[k<<1],tree[k<<1|1]); } void build(int l,int r,int k) { if(l==r) { tree[k]=0; return; } int m=(l+r)>>1; build(lson); build(rson); pushup(k); } int query_getmax(int x,int y,int l,int r,int k)//这里是求出最大值 { if(x<=l&&r<=y) return tree[k]; int m=(l+r)>>1; int ans=0; if(x<=m) ans=max(ans,query_getmax(x,y,lson)); if(y>m) ans=max(ans,query_getmax(x,y,rson)); return ans; } void update(int p,int c,int l,int r,int k) { if(l==r) { tree[k]=c; return; } int m=(l+r)>>1; if(p<=m) update(p,c,lson); else update(p,c,rson); pushup(k); } struct point{ int x,y,w; int ha_x,ha_y;//记录离散后的坐标 }a[100005]; bool cmp1(point a,point b) { return a.x<b.x; } bool cmp2(point a,point b) { return a.y<b.y; } bool cmp3(point a,point b) { if(a.x==b.x) return a.y>b.y; return a.x<b.x; } int main() { int n,m; int t; scanf("%d",&t); while(t--) { scanf("%d",&n); int ha=0; build(1,n,1); for(int i=1;i<=n;i++) scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w); sort(a+1,a+1+n,cmp1); int haxx=1; for(int i=1;i<=n;i++)//对x坐标得点进行离散化 { a[i].ha_x=haxx; if(a[i+1].x!=a[i].x)haxx++; } int hayy=1; sort(a+1,a+1+n,cmp2); for(int i=1;i<=n;i++)//对y坐标进行离散化 { a[i].ha_y=hayy; if(a[i+1].y!=a[i].y)hayy++; } sort(a+1,a+1+n,cmp3); haxx--,hayy--; for(int i=1;i<=n;i++)//跟一维01背包的更新可像 { if(a[i].ha_y==1) update(a[i].ha_y,a[i].w,1,n,1);//到了最下面,在1的线段树出更新 else { int t=query_getmax(1,a[i].ha_y-1,1,n,1)+a[i].w;//从1到a[i].ha_y 这个区间求出最大值 update(a[i].ha_y,t,1,n,1);//把这个点的最优值储存在a[i].ha_y上 } } cout<<query_getmax(1,n,1,n,1)<<endl; } return 0; }