Codeforces Round #278 (Div. 1) Strip RMQ


B. Strip
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output

Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right.

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

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

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

Input

The first line contains three space-separated integers n, s, l (1 ≤ n ≤ 105, 0 ≤ s ≤ 109, 1 ≤ l ≤ 105).

The second line contains n integers ai separated by spaces ( - 109 ≤ ai ≤ 109).

Output

Output the minimal number of strip pieces.

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

Examples
Input
7 2 2
1 3 1 2 4 1 2
Output
3
Input
7 2 2
1 100 1 100 1 100 1
Output
-1
Note

For the first sample, we can split the strip into 3 pieces: [1, 3, 1], [2, 4], [1, 2].

For the second sample, we can't let 1 and 100 be on the same piece, so no solution exists.

题目大意:让你把一个序列分几段,每段的最大值和最小值之差不超过s, 该段的长度也必须大于等于l,问你最少能分成几段?

这当题当然是要dp啦!反正我没想出来怎么划分。这题真是很棒!

首先给出dp公式 dp[i] = min{dp[j]+1, dp[i]}, i-j >= l && max{j+1~i}-min{j+1,~i}<= s

满足这个条件就可以dp了!用rmq求最大最小的差值, 双指针维护左右两个点i,j序列。

#include <bits/stdc++.h>

using namespace std;
const int MAXN = 100005;
const int INF = 0x3f3f3f3f;
int f[MAXN][20], g[MAXN][20], a[MAXN], dp[MAXN];
int n, q;
void Rmq() {
    int lg = floor(log10(double(n))/log10(double(2)));
    for(int j = 1; j <= lg; ++j) {
        for(int i = 1; i <= n+1-(1<<j); ++i) {
            g[i][j] = max(g[i][j-1], g[i+(1<<(j-1))][j-1]);
            f[i][j] = min(f[i][j-1], f[i+(1<<(j-1))][j-1]);
        }
    }
}
int query(int a, int b) {

    int lg = floor(log10(double(b-a+1))/log10(double(2)));
    return max(g[a][lg], g[b-(1<<lg)+1][lg])
           -  min(f[a][lg], f[b-(1<<lg)+1][lg]);
}
int main() {
    int l, s;
    scanf("%d%d%d", &n, &s, &l);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        f[i][0] = g[i][0] = a[i];
        dp[i] = INF;
    }
    Rmq();
    dp[0] = 0;
    int pre = 0;
    for(int i = l; i <= n; i++) {
        while((i-pre >= l && query(pre+1, i) > s )|| dp[pre] == INF) pre++;
        printf("%d %d\n", pre, i);
        if(i - pre >= l)
            dp[i] = min(dp[pre]+1, dp[i]);
    }
    if(dp[n] == INF) printf("-1\n");
    else printf("%d\n", dp[n]);
    return 0;
}
View Code

优质内容筛选与推荐>>
1、ElasticSearch文档
2、云南大理怎么玩最省钱 两日游实用攻略
3、找不到mysql服务或mysql服务名无效
4、C++小知识之Vector用法
5、ScrollView


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn