YJJ's SalesmanTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 741 Accepted Submission(s): 211 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. Input The first line of the input contains an integer T (1≤T≤10) ,which is the number of test cases. Output The maximum of dollars YJJ can get. Sample Input 1 3 1 1 1 1 2 2 3 3 1 Sample Output 3 |
题意:
给你n个村庄的坐标和到达这些村庄可获得的值,但是只能从(xi-1,yi-1)这个坐标达到村庄(x,y),同时如果你在(x,y),你的下一步只能走到(x+1,y),(x,y+1),(x+1,y+1)。问你能获得的最大权值。
做法:
树状数组,更新和询问的都是最大值,向上更新向下询问,所以一开始的时候只要把y坐标进行离散化,用x从小到大,x相同的时候y从小到大来排序,一步步query和add就可以了。具体见代码。
代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn=100005;
struct node{
int x,y,v;
}e[maxn];
bool cmp1(node a,node b){
return a.y<b.y;
}
bool cmp2(node a,node b){
if(a.x==b.x) return a.y>b.y;
return a.x<b.x;
}
int a[maxn];
ll aim[maxn];
int lowbit(ll x){
return x&(-x);
}
void add(ll x,int pos){
while(pos<=maxn){
aim[pos]=max(aim[pos],x);
pos+=lowbit(pos);
}
}
ll query(int pos){
ll ans=0;
while(pos){
ans=max(aim[pos],ans);
pos-=lowbit(pos);
}
return ans;
}
int main(){
int x,y,t,v,n;
cin>>t;
while(t--){
memset(aim,0,sizeof(aim));
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].v);
a[i]=e[i].y;
}
sort(e+1,e+1+n,cmp1);
sort(a+1,a+1+n);
int len=unique(a+1,a+1+n)-a;
for(int i=1;i<=n;i++){
e[i].y=lower_bound(a+1,a+1+len,e[i].y)-a;
}
sort(e+1,e+1+n,cmp2);
for(int i=1;i<=n;i++){
ll now=query(e[i].y-1)+e[i].v;
add(now,e[i].y);
}
ll ans=query(maxn-1);
printf("%lld\n",ans);
}
return 0;
}