http://acm.hdu.edu.cn/showproblem.php?pid=6447
YJJ's SalesmanTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1330 Accepted Submission(s): 470 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
Source
Recommend chendu | We have carefully selected several similar problems for you: 6447 6446 6445 6444 6443
|
思路:DP+线段树+离散化,当初想的时候只想到了如何找出i结点之前的比这个结点y值小的的最大值如何用线段树求出,想不出来就放弃了线段树的思路。原来答案是高度1~y-1的最大值用线段树保存。真是菜鸡
AC代码
#include<bits/stdc++.h>
using namespace std;
#define N 100005
struct node{
long long x=0;
long long y=0;
long long v=0;
};
node nodes[N];
long long maxn[N<<2];
int posHash[N];
long long maxy;
void init(int n)
{
sort(nodes+1,nodes+1+n,[](const node a,const node b){return a.x<b.x;});
posHash[0]=1;
for(int i=1;i<=n;++i)
{
if(nodes[i].x==nodes[i-1].x)
posHash[i]=posHash[i-1];
else
posHash[i]=posHash[i-1]+1;
}
for(int i=1;i<=n;++i)
{
nodes[i].x=posHash[i];
}//x值数据离散化
sort(nodes+1,nodes+1+n,[](const node a,const node b){return a.y<b.y;});
posHash[0]=1;
for(int i=1;i<=n;++i)
{
if(nodes[i].y==nodes[i-1].y)
posHash[i]=posHash[i-1];
else
posHash[i]=posHash[i-1]+1;
}
for(int i=1;i<=n;++i)
nodes[i].y=posHash[i];
maxy=posHash[n];//y值数据离散化
}
void build(int l,int r,int rt)
{
if(l==r)
maxn[rt]=0;
else
{
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
maxn[rt]=max(maxn[rt<<1],maxn[rt<<1|1]);
}
}
void update(int L,int C,int l,int r,int rt)
{
if(l==r)
{
maxn[rt]=max(maxn[rt],(long long)C);
return;
}
else
{
int mid=(l+r)>>1;
if(L<=mid)
update(L,C,l,mid,rt<<1);
else
update(L,C,mid+1,r,rt<<1|1);
maxn[rt]=max(maxn[rt<<1],maxn[rt<<1|1]);
}
}
long long query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
return maxn[rt];
}
else
{
long long ans=0;
int mid=(l+r)>>1;
if(L<=mid)
ans=max(ans,query(L,R,l,mid,rt<<1));
if(R>mid)
ans=max(ans,query(L,R,mid+1,r,rt<<1|1));
return ans;
}
}
int main()
{
std::ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
for(int i=1;i<=n;++i)
cin>>nodes[i].x>>nodes[i].y>>nodes[i].v;
init(n);
sort(nodes+1,nodes+1+n,[](const node a,const node b){return a.x==b.x?a.y>b.y:a.x<b.x;});
build(1,maxy,1);
long long result=0;
for(int i=1;i<=n;++i)
{
long long ans=0;
if(nodes[i].y==1)
ans=nodes[i].v;
else
ans=query(1,nodes[i].y-1,1,maxy,1)+nodes[i].v;
result=max(result,ans);
update(nodes[i].y,ans,1,maxy,1);
}
cout<<result<<endl;
}
}