题外话
历史上最接近 AK 的一次,但是眼残的我把:
and
看成了:
and
当时就给我整懵了,这什么逆天题啊,不会做,根本不会做。然后过去把 G 题秒掉了(题外话的题外话:讨论区有人说 G 题有原题,那岂不是原题机er 秒杀我们这些正经打的,气死)。结果比赛结束一看,跟我水平差不多的几乎都是 ABCDEF,回来看 F,(一种单子叶植物),读错题了。
题解
如果你读对题了,很难不发现这里的 具有决策单调性,因此考虑 DP。为了让这个 DP 更正常一点,我构造了一个新的 数组,满足 。让 表示经过了 的路径中最长的长度,很难不想到转移方程:
但是这样是 的,看到这里的区间最大值,很难不想到使用线段树,然后每次枚举到 的时候才把 加到 号元素,这样就可以保证 的条件了。最后输出 数组中的最大值减一(因为是算边数而不是点数)即可。
代码:
namespace AC{
struct node{
int l,r,mx;
}tr[2000010];
void build(int p,int l,int r){
tr[p].l=l,tr[p].r=r;
if(l==r)return;
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
}
void update(int p,int x,int v){
if(tr[p].l==tr[p].r){
tr[p].mx=v;
return;
}
int mid=(tr[p].l+tr[p].r)>>1;
if(x<=mid)update(p<<1,x,v);
else update(p<<1|1,x,v);
tr[p].mx=max(tr[p<<1].mx,tr[p<<1|1].mx);
}
int query(int p,int l,int r){
if(tr[p].l>=l&&tr[p].r<=r)return tr[p].mx;
int mid=(tr[p].l+tr[p].r)>>1;
int res=0;
if(l<=mid)res=max(res,query(p<<1,l,r));
if(r>mid)res=max(res,query(p<<1|1,l,r));
return res;
}
int n,d,r;
int h[500010],dp[500010];
void solve(){
cin>>n>>d>>r;
for(int i=1;i<=n;i++){
int x;
cin>>x;
h[x]=i;
}
build(1,1,n);
int ans=0;
for(int i=1;i<=n;i++){
if(i-d>=1)update(1,h[i-d],dp[i-d]);
dp[i]=query(1,max(1ll,h[i]-r),min(n,h[i]+r))+1;
}
for(int i=1;i<=n;i++)ans=max(ans,dp[i]);
cout<<ans-1<<endl;
}
}