CF 487B Strip(线段树优化DP)

CF 487B Strip

题目描述

Alexandra has a paper strip with n n n numbers on it. Let’s call them a i a_{i} ai from left to right.
Now Alexandra wants to split it into some pieces (possibly 1 1 1 ). For each piece of strip, it must satisfy:

  • Each piece should contain at least l l l numbers.
  • The difference between the maximal and the minimal number on the piece should be at most s s s.
    Please help Alexandra to find the minimal number of pieces meeting the condition above.
输入格式

Alexandra has a paper strip with n n n numbers on it. Let’s call them a i a_{i} ai from left to right.

Now Alexandra wants to split it into some pieces (possibly 1 1 1). For each piece of strip, it must satisfy:

  • Each piece should contain at least l l l numbers.
  • The difference between the maximal and the minimal number on the piece should be at most s s s.

Please help Alexandra to find the minimal number of pieces meeting the condition above.

输出格式

Output the minimal number of strip pieces.

If there are no ways to split the strip, output -1.

Sample Input 1
7 2 2
1 3 1 2 4 1 2
Sample Output 1
3
Sample Input 2
7 2 2
1 100 1 100 1 100 1
Sample Input 2
-1

1 ≤ n , l ≤ 1 0 5 , 1 ≤ s ≤ 1 0 9 , − 1 0 9 ≤ a i ≤ 1 0 9 1\leq n,l\leq 10^5,1\leq s\leq 10^9,-10^9\leq a_i\leq 10^9 1n,l105,1s109,109ai109

题目大意

给出一个长度为 n n n的数组,要求将其按原来的顺序分成若干份且各部分满足以下两个条件:

  • 每个部分至少有 l l l个元素
  • 每个部分中最大值和最小值之差不能超过 s s s

问最多可以将这些元素分成几个部分。如果无法分这个数组,就输出-1

题解

前置知识:线段树优化DP
很容易推出状态转移式为:

f i = max ⁡ f j + 1 f_i=\max f_j+1 fi=maxfj+1,其中 j j j满足 1 ≤ j ≤ i − l 1\leq j\leq i-l 1jil j + 1 j+1 j+1 i i i的最大值与最小值之差小于等于 s s s

首先,我们可以用单调队列来维护目前区间的最大值和最小值,如果两者差大于 s s s则将区间左结点向右移,直到差小于等于 s s s

然后,我们可以将 a i a_i ai离散化一下,再用权值线段树来维护值为 k k k a i a_i ai的最大的 f i f_i fi的值。对于每个 f i f_i fi,在线段树上查找对应的位置中的最大值,用这个值加一来更新 f i f_i fi,再用 f i f_i fi更新 a i a_i ai位置即可。

单调队列平摊下来是 O ( n ) O(n) O(n)的,线段树区间查询和单点修改都是 O ( n l o g n ) O(nlogn) O(nlogn),所以总时间复杂度为 O ( n l o g n ) O(nlogn) O(nlogn)

code

#include<iostream>
#include<cstdio>
#define lc k<<1
#define rc k<<1|1
using namespace std;
int n,s,l,a1=1,a2=0,b1=1,b2=0,sum,va=0,vb=0,inf=1000000000,a[100005],f[100005],ha[100005],hb[100005],tr[400005];
void build(int k,int l,int r){
    tr[k]=inf;
    if(l==r) return;
    int mid=(l+r)/2;
    build(lc,l,mid);build(rc,mid+1,r);
}
void pt(int k,int l,int r,int x,int y){
    if(l==r&&l==x){
        tr[k]=y;
        return;
    }
    if(l>x||r<x) return;
    if(l==r) return;
    int mid=(l+r)/2;
    if(x<=mid) pt(lc,l,mid,x,y);
    else pt(rc,mid+1,r,x,y);
    tr[k]=min(tr[lc],tr[rc]);
}
void find(int k,int l,int r,int x,int y){
    if(l>=x&&r<=y){
        sum=min(sum,tr[k]);
        return;
    }
    if(l>y||r<x) return;
    if(l==r) return;
    int mid=(l+r)/2;
    if(x<=mid) find(lc,l,mid,x,y);
    if(y>mid) find(rc,mid+1,r,x,y);
}
int main()
{
    while(scanf("%d%d%d",&n,&s,&l)!=EOF){
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            f[i]=inf;
        }
        a1=1,a2=0,b1=1,b2=0,va=0,vb=0;
        build(1,1,n);
        for(int i=1;i<l;i++){
            while(a1<=a2&&a[ha[a2]]<a[i]) --a2;
            ha[++a2]=i;
            while(b1<=b2&&a[hb[b2]]>a[i]) --b2;
            hb[++b2]=i;
        }
        for(int i=l;i<=n;i++){
            while(a1<=a2&&a[ha[a2]]<a[i]) --a2;
            ha[++a2]=i;
            while(b1<=b2&&a[hb[b2]]>a[i]) --b2;
            hb[++b2]=i;
            while(a1<=a2&&b1<=b2&&a[ha[a1]]-a[hb[b1]]>s){
                if(ha[a1]<hb[b1]) va=ha[a1],++a1;
                else vb=hb[b1],++b1;
            }
            int lt=max(va,vb),rt=i-l;
            if(lt==0) f[i]=1;
            else{
                sum=inf;
                if(lt<=rt) find(1,1,n,lt,rt);
                f[i]=sum+1;
            }
            pt(1,1,n,i,f[i]);
        }
        if(f[n]>=inf) printf("-1");
        else printf("%d",f[n]);
    }
    return 0;
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值