dp好想。bit的优化好想。还有细节:
(1)从k->0,这样才不会被本身转移。
(2)这个dp表示的是以i结尾的最长的长度,所以随时max。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,k,tab[][],dp[][],h[],mx=,ans=;
int lowbit(int x) {return (x&(-x));}
int ask(int x,int y)
{
int ret=;
for (int i=x;i>=;i-=lowbit(i))
for (int j=y;j>=;j-=lowbit(j))
ret=max(ret,tab[i][j]);
return ret;
}
void insert(int x,int y,int val)
{
for (int i=x;i<=mx;i+=lowbit(i))
for (int j=y;j<=k+;j+=lowbit(j))
tab[i][j]=max(tab[i][j],val);
}
int main()
{
scanf("%d%d",&n,&k);
for (int i=;i<=n;i++) scanf("%d",&h[i]);
for (int i=;i<=n;i++) mx=max(mx,h[i]);mx+=k;
for (int i=;i<=n;i++)
for (int j=k;j>=;j--)
{
dp[i][j]=ask(h[i]+j,j+)+;
insert(h[i]+j,j+,dp[i][j]);
ans=max(ans,dp[i][j]);
}
printf("%d\n",ans);
return ;
}