YJJ's SalesmanTime 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.
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 |
題意:
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;
}