题目链接:https://cn.vjudge.net/problem/HYSBZ-2131

题目大意:中文题目

具体思路:对于任意的两个位置,posA和posB,我们可以如下推导。

|posA-posB|<=2*(tA-tB)

2*tB-2*tA<=posA-posB<=2*tB-2*tA.

2*tA+posA>=2*tB+posB  || 2*tA -pos A > = 2*tB-posB.(注意这个地方,只要有一个满足就可以了)

然后我们对2*tA+posA进行排序,每一次询问当当前能到达的最远的,这一段的最大值就可以了。

AC代码:

 #include<bits/stdc++.h>
using namespace std;
# define ll long long
const int maxn = 1e5+;
struct node
{
int w1,w2,val;
bool friend operator < (node t1,node t2)
{
if(t1.w1==t2.w1)
return t1.w2<t2.w2;
return t1.w1<t2.w1;
}
} q[maxn];
int a[maxn],ans[maxn],sz,c[maxn];
int lowbit(int t)
{
return t&(-t);
}
int query(int t)
{
int maxx=;
for(int i=t; i; i-=lowbit(i))
{
maxx=max(maxx,c[i]);
}
return maxx;
}
void update(int pos,int val)
{
for(int i=pos; i<=sz; i+=lowbit(i))
{
c[i]=max(c[i],val);
}
}
int main()
{
int n,m,t,p;
scanf("%d %d",&n,&m);
for(int i=; i<=m; i++)
{
scanf("%d %d %d",&t,&p,&q[i].val);
q[i].w1=*t-p;
q[i].w2=*t+p;
}
sort(q+,q+m+);
for(int i=; i<=m; i++)
{
a[i]=q[i].w2;
}
sort(a+,a+m+);//注意离散化的时候要进行排序
sz=unique(a+,a+m+)-a-;
for(int i=; i<=m; i++)
{
q[i].w2=lower_bound(a+,a+sz+,q[i].w2)-a;
ans[i]=query(q[i].w2)+q[i].val;
update(q[i].w2,ans[i]);
}
int maxx=;
for(int i=; i<=m; i++)
{
maxx=max(maxx,ans[i]);
}
printf("%d\n",maxx);
return ;
}
05-11 11:32
查看更多