hdu 5489 Removed Interval 2015合肥网络赛 树状数组 dp 离散化/dp

探讨了在移除长度为L的子序列后,如何高效计算剩余序列的最长递增子序列(LIS)的最大长度。通过使用树状数组和离散化技巧,实现了O(n log n)的时间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5489

题目来源:2015合肥网络赛第三题。题目简洁处理起来挺巧妙的。

简要题意:求长度为 N 的序列移掉长度为L的一段后LIS的最大长度。

数据范围: T100;ΣN5000001N105;0LN;Ai109;

题解

维护一个树状数组保存query(x)求出结尾不超过 x 含有L空隙的最长LIS的长度。

对于当前位置来说以当前值为结尾的LIS共有两种来源:

第一种是前面含有 L 空隙,这种就是query(a[i]-1)+1,取出结尾严格小于a[i]的最大的LIS接上。

第二种是在最后插入一个 L 空隙,这个需要用到[1,iL]的LIS信息,这个直接顺便做就行了。

UPD:由于GDUT某人给了我一份精彩的代码,我看完之后理解了下补在了后面。

实现

首先要会 Θ(nlogn) 求LIS的方法也就是lower_bound()乱搞。

然后 Ai 比较大所以要离散化一下。

经过这场比赛也算是会用点树状数组了。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 1e5+5;
const int INF = 0x3f3f3f3f;

struct BIT {
    int tree[N] ;

    int lowbit(int x) {
        return x&(-x);
    }
    void add(int x, int add, int n) {
        for (; x <= n; x += lowbit(x)) {
            tree[x] = max(add, tree[x]);
        }
    }
    int sum(int x) {
        int s = 0;
        for (; x > 0; x -= lowbit(x)) {
            s = max(s, tree[x]);
        }
        return s;
    }
    void clear() {
        memset(tree, 0, sizeof tree);
    }
};
BIT tree;
int lis[N];
int a[N];
int dis[N];

int main()
{
    int t, n, l, cas = 1;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n, &l);
        memset(lis, INF, sizeof lis);
        for (int i = 1; i <= n; i++) {
            scanf("%d", a+i);
            dis[i] = a[i];
        }
        sort(dis+1, dis+n+1);
        int tot = unique(dis+1, dis+n+1)-dis-1;
        for (int i = 1; i <= n; i++) {
            a[i] = lower_bound(dis+1, dis+1+tot, a[i])-dis;
        }

        for (int i = l+1; i <= n; i++) {
            int pos = lower_bound(lis+1, lis+tot+1, a[i-l])-lis;
            lis[pos] = a[i-l];
            tree.add(a[i], tree.sum(a[i]-1)+1, tot);
            tree.add(a[i-l], pos, tot);
        }

        printf("Case #%d: %d\n", cas++, tree.sum(tot));
        tree.clear();
    }
    return 0;
}

脑洞题解

GDUT某人很神奇地几行代码就A掉了这题,我就学习了下。先称题目中的lis为glis吧。

我们先假设之前的glis被正确更新。

首先如果之前已经有了间隙,glis前面的部分一定是个曾经更新过的glis,保存在里面。

然后如果当前插入一个空隙,它是一个glis,那去掉末尾那个,之前的也一定是个glis,也被保存了。

如果我们进行更新,保证之前的glis都被更新了,那么只需要对glis像对普通lis那样先更新一次。

然后再根据 a1ail ail 结尾的lis去更新glis,由于之前的glis已经完全更新,我们只需要去看当前lis的位置和当前glis对应的位置进行更新即可。

于是我们就得到了新的正确的glis。

整个思考是一个数归的思考方式,代码很短,思路很脑洞也很神奇。

脑洞代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 1E5+5;
const int INF = 0x3f3f3f3f;

int lis[N];
int glis[N];
int a[N];

int main()
{
    int t, cas = 1, n, l;
    scanf("%d", &t);
    while (t--) {
        memset(lis, INF, sizeof lis);
        memset(glis, INF, sizeof glis);
        scanf("%d%d", &n, &l);
        for (int i = 0; i < n; i++) {
            scanf("%d", a+i);
        }
        for (int i = l; i < n; i++) {
            *lower_bound(glis, glis+n, a[i]) = a[i];
            int pos = lower_bound(lis, lis+n, a[i-l])-lis;
            lis[pos] = a[i-l];
            glis[pos] = min(lis[pos], glis[pos]);
        }
        printf("Case #%d: %d\n", cas++, lower_bound(glis, glis+n, INF)-glis);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值