https://lydsy.com/JudgeOnline/problem.php?id=5442php
分析:spa
代碼:code
#include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> using namespace std; #define N 400050 int V[N],n,X,a[N],c[N],L[N],R[N],ln,f[N]; void fix(int x,int v) { for(;x<=ln;x+=x&(-x)) c[x]=max(c[x],v); } int inq(int x) { int re=0; for(;x;x-=x&(-x)) re=max(re,c[x]); return re; } void fix2(int x,int v) { for(;x;x-=x&(-x)) c[x]=max(c[x],v); } int inq2(int x) { int re=0; for(;x<=ln;x+=x&(-x)) re=max(re,c[x]); return re; } int main() { scanf("%d%d",&n,&X); int i; for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=1;i<=n;i++) a[i+n]=a[i]+X; ln=n<<1; for(i=1;i<=ln;i++) V[i]=a[i]; sort(V+1,V+ln+1); int cnt=unique(V+1,V+ln+1)-V-1; for(i=1;i<=ln;i++) a[i]=lower_bound(V+1,V+cnt+1,a[i])-V; for(i=1;i<=n;i++) { L[i]=inq(a[i+n]-1)+1; f[i]=inq(a[i]-1)+1; fix(a[i],f[i]); } memset(c,0,sizeof(c)); for(i=n;i;i--) { f[i]=inq2(a[i]+1)+1; fix2(a[i],f[i]); } int ans=0; for(i=1;i<=n;i++) ans=max(ans,L[i]+f[i]); printf("%d\n",ans-1); }