标签:des style blog http color io os ar java
链接:
题目:中文题目
思路:
这个题目如果去掉那个距离大于d的条件,那么必然是一个普通的LIS,但是加上那个条件后就变得复杂了。用dp的解法没有看懂,我用的线段树的解法。。。就是采用延迟更新的做法,用为距离要大于d啊,所以我们在循环到第i的时候,就对(i-d-1)这个点进行更新,因为如果在(i-d-1)这个点更新了,会对后面的造成影响,然后线段树的tree【】数组存的是以i结尾的最长lis,那么每次询问的时候就找最大的tree【】就可以了。。。
代码:
2 0 1 2 5 1 3 4 5 1 2 5 2 3 4 5 1 2
2 2 1
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
#include<string>
#include<queue>
#define eps 1e-9
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=100000+10;
int a[maxn],dp[maxn],n,d;//表示以i结尾的LIS
int tree[maxn<<2];
void push_up(int dex)
{
tree[dex]=max(tree[dex<<1],tree[dex<<1|1]);
}
void buildtree(int l,int r,int dex)
{
tree[dex]=0;
if(l==r) return;
int mid=(l+r)>>1;
buildtree(l,mid,dex<<1);
buildtree(mid+1,r,dex<<1|1);
}
void Update(int pos,int l,int r,int dex,int value)
{
if(l==r)
{
tree[dex]=max(tree[dex],value);
return;
}
int mid=(l+r)>>1;
if(pos<=mid) Update(pos,l,mid,dex<<1,value);
else Update(pos,mid+1,r,dex<<1|1,value);
push_up(dex);
}
int Query(int l,int r,int L,int R,int dex)
{
if(L<=l&&R>=r) return tree[dex];
int mid=(l+r)>>1;
if(R<=mid) return Query(l,mid,L,R,dex<<1);
else if(L>mid) return Query(mid+1,r,L,R,dex<<1|1);
else return max(Query(l,mid,L,R,dex<<1),Query(mid+1,r,L,R,dex<<1|1));
}
int main()
{
int temp,ans;
while(~scanf("%d%d",&n,&d))
{
ans=temp=-1;
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
temp=max(temp,a[i]);
}
buildtree(0,temp,1);
for(int i=1;i<=n;i++)
{
if(i-d-1>=1) Update(a[i-d-1],0,temp,1,dp[i-d-1]);
if(a[i]>=1) dp[i]=Query(0,temp,0,a[i]-1,1)+1;
else dp[i]=1;
ans=max(ans,dp[i]);
}
printf("%d\n",ans);
}
return 0;
}
hdu4521 小明系列问题——小明序列(LIS变种 (线段树+单点更新解法))
标签:des style blog http color io os ar java
原文地址:http://blog.csdn.net/u014303647/article/details/40436405