zoukankan      html  css  js  c++  java
  • 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
    ", pre, i);
            if(i - pre >= l)
                dp[i] = min(dp[pre]+1, dp[i]);
        }
        if(dp[n] == INF) printf("-1
    ");
        else printf("%d
    ", dp[n]);
        return 0;
    }
    View Code
  • 相关阅读:
    一个简单的linux线程池(转-wangchenxicool)
    Linux下获得系统时间的C语言实现
    C语言实现简单线程池(转-Newerth)
    C语言实现Web客户端(转-kungstriving)
    C语言实现的Web服务器(转-kungstriving)
    linux和window下mkdir函数问题(转-锦曦月)
    linux C 获取当前目录的实现(转-Blossom)
    linux C之access函数(转-追梦的小鸟)
    Linux C 创建目录函数mkdir相关(转-清新居士)
    50个C/C++源代码网站(转-清风小阁)
  • 原文地址:https://www.cnblogs.com/cshg/p/6618872.html
Copyright © 2011-2022 走看看