BZOJ 1109: [POI2007]堆积木Klo LIS

本文探讨了在特定条件下寻找最长递增子序列(LIS)的问题,并通过巧妙地利用一维和二维偏序特性简化问题。针对输入序列,文章提出了一种有效的算法实现方案,通过维护一个树状数组来动态更新最长递增子序列的长度。

时空隧道


首先我们要保证a[i]是递增的…然后还有保证i-a[i]也是不减的…
然后我们发现这是一个三维偏序的东西…然后再看一看i-a[i]发现其实是个二维偏序,因为a[i]递增i-a[i]不减的时候i必定递增…
所以我们有a[i]作为下标,求一个LIS就可以了


代码如下:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1000000+5;
int n,tr[maxn],cnt,maxnum;
struct M{
    int x,y;
    friend bool operator < (M a,M b){
        if(a.x==b.x)
            return a.y<b.y;
        return a.x<b.x;
    }
}s[maxn];
inline void insert(int x,int y){
    for(;x<=maxnum;x+=x&(-x))
        tr[x]=max(tr[x],y);
}
inline int query(int x){
    int res=0;
    for(;x;x-=x&(-x))
        res=max(res,tr[x]);
    return res;
}
signed main(void){
    scanf("%d",&n);cnt=0;maxnum=0;
    for(int i=1,x;i<=n;i++){
        scanf("%d",&x);
        if(i>=x)
            s[++cnt].x=i-x,s[cnt].y=x,maxnum=max(maxnum,x);
    }sort(s+1,s+cnt+1);int ans=0;
    for(int i=1;i<=cnt;i++){
        int tmp=query(s[i].y-1)+1;
        ans=max(ans,tmp);insert(s[i].y,tmp);
    }
    printf("%d\n",ans);
    return 0;
}

by >_< NeighThorn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值