题目链接

https://www.lydsy.com/JudgeOnline/problem.php?id=5442

题解

我们可以发现两个性质:

  1. 在[l,r]+1,不如在[l,n]+1;在[l,r]-1,不如在[1,r]-1。
  2. 在[1,r]-1,可以转化成在(r,n]+1。

因此可以将所有操作转化成在[l,n]+1,这样我们可以记录一个f[x],代表正好在[x,n]+1,并且必须选取x(因为如果不选x完全可以在>x的位置+1的)时,[1,x-1]的LIS长度。求完f[x]之后再从n到1求一遍LDS就可以了。

代码

#include <cstdio>
#include <algorithm>

int read()
{
  int x=0,f=1;
  char ch=getchar();
  while((ch<'0')||(ch>'9'))
    {
      if(ch=='-')
        {
          f=-f;
        }
      ch=getchar();
    }
  while((ch>='0')&&(ch<='9'))
    {
      x=x*10+ch-'0';
      ch=getchar();
    }
  return x*f;
}

const int maxn=200000;

int n,x,v[maxn+10],f[maxn+10],g[maxn+10],cnt,ans;

int main()
{
  n=read();
  x=read();
  for(int i=1; i<=n; ++i)
    {
      v[i]=read();
    }
  for(int i=1; i<=n; ++i)
    {
      int pos=std::lower_bound(f+1,f+cnt+1,v[i]+x)-f-1;
      g[i]=pos;
      pos=std::lower_bound(f+1,f+cnt+1,v[i])-f;
      f[pos]=v[i];
      if(pos>cnt)
        {
          cnt=pos;
        }
    }
  ans=cnt;
  cnt=0;
  for(int i=n; i; --i)
    {
      int pos=std::lower_bound(f+1,f+cnt+1,-v[i])-f;
      f[pos]=-v[i];
      ans=std::max(ans,g[i]+pos);
      if(pos>cnt)
        {
          cnt=pos;
        }
    }
  printf("%d\n",ans);
  return 0;
}