BZOJ5442 [Ceoi2018]Global warming

本文探讨了在给定数组中,允许元素在一定范围内调整,求解经调整后的最长严格上升子序列的问题。通过使用动态规划和树状数组,实现了时间复杂度为O(nlogn)的高效算法。

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

标签:LIS,DP,树状数组

题目

题目传送门

Description

给定 n ( n ≤ 200 , 000 ) n(n\leq 200,000) n(n200,000),你可以将任意 a [ l ] a[l] a[l] a [ r ] ( 1 ≤ l ≤ r ≤ n ) a[r](1\leq l\leq r\leq n) a[r](1lrn)每一个元素加上一个 d ( − x ≤ d ≤ x ) d(-x\leq d\leq x) d(xdx),求 a a a数组的最大严格上升子序列长度。

Input

第1行 两个整数 n , x ; n,x; n,x;

第2行 n n n个整数表示 a [ 1 ] a[1] a[1] a [ n ] a[n] a[n]

Output

一个数,即 a a a数组的最大严格上升子序列长度。

Sample Input

8 10
7 3 5 12 2 7 3 4

Sample Output

5

题解

看数据范围2e5,时间复杂度应该就是 O ( n log ⁡ n ) O(n\log n) O(nlogn)

首先你要知道几个性质:

  • 如果在 [ l , r ] + n u m [l,r]+num [l,r]+num,不如在 [ l , n ] + n u m [l,n]+num [l,n]+num
  • 如果在 [ l , r ] − n u m [l,r]-num [l,r]num,不如在 [ 1 , r ] − n u m [1,r]-num [1,r]num,不如在 ( r , n ] + n u m (r,n]+num (r,n]+num
  • 如果在 [ l , r ] + 1 [l,r]+1 [l,r]+1,不如在 [ l , r ] + m a x [l,r]+max [l,r]+max,即选定区间加最大值

问题转化为:给你一个序列,可以在 [ x , n ] [x,n] [x,n]范围内加上 m m m,求LIS

之后就正着跑和反着跑,各求一遍LIS,最后用树状数组合并

code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
#define lowbit(x) (x&(-x))
using namespace std;
inline ll read(){
    ll f=1,x=0;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//**********head by yjjr**********
const int maxn=2e5+6;
int top,sta[maxn],sum[maxn<<1],n,x,a[maxn],b[maxn<<1],c[maxn],f1[maxn],f2[maxn],ans=0,pos;
void modify(int x,int val){for(int i=x;i<=2*n;i+=lowbit(i))sum[i]=max(sum[i],val);}
inline int getmax(int x){int re=0;for(int i=x;i;i-=lowbit(i))re=max(re,sum[i]);return re;}
int main(){
    n=read(),x=abs(read());
    rep(i,1,n)a[i]=b[i]=c[i]=read(),b[i+n]=a[i]+x-1;
    sort(b+1,b+1+n*2);
    rep(i,1,n)a[i]=lower_bound(b+1,b+1+2*n,a[i])-b; 
    rep(i,1,n)
        if(a[i]>sta[top])sta[++top]=a[i],f1[i]=top;
        else{pos=lower_bound(sta+1,sta+1+top,a[i])-sta;sta[pos]=a[i],f1[i]=pos;}
    top=0;
    dep(i,n,1){
        int tmp=2*n+1-a[i];
        if(tmp>sta[top])sta[++top]=tmp,f2[i]=top;
        else{pos=lower_bound(sta+1,sta+1+top,tmp)-sta;sta[pos]=tmp,f2[i]=pos;}
    }
    modify(a[1],f1[1]);
    rep(i,2,n){
        int t=lower_bound(b+1,b+1+2*n,c[i]+x-1)-b,post=getmax(t);
        if(f2[i]+post>ans)ans=f2[i]+post,pos=t;
        modify(a[i],f1[i]);
    }
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值