[P2034] select the digital Luo Valley

Description

[P2034] select the digital Luo Valley

Given a sequence, wherein the number select a number, but can not have more than k consecutive numbers are selected. Selected to maximize the sum of the numbers.

Solution

Monotone queue dp +

N is anti difficult, considering the number taken from the sequence number, the distance between the pairwise number than k, and minimization of

Then define $ F [i] $ i represents the number of the former, and the minimum number of taken (taken to ensure that the i-th), then clearly there is a state transition equation

$$f[i]=\min\{f[j]\}+a[i]\quad (i-k-1\le j<i)$$

Can be found, for each i, j in the decision to allow the collection period are continuous, and when increasing i, the decision will be allowed to overlap a portion of the previous set, and the transfer is determined most value, with a maintenance queue monotone look like,

Time complexity is $ O (n) $

Code

#include <bits/stdc++.h>
// check if it is judged online

namespace shl {
    typedef long long ll;
    inline int read() {
        int ret = 0, op = 1;
        char c = getchar();
        while (!isdigit(c)) {
            if (c == '-') op = -1;
            c = getchar();
        }
        while (isdigit(c)) {
            ret = ret * 10 + c - '0';
            c = getchar();
        }
        return ret * op;
    }
    int n, m;
    ll a[100010], sum, f[100010], que[100010], h = 1, t = 1, tot;
    int main() {
        n = read(), m = read();
        for (register int i = 1; i <= n; ++i) a[i] = read(), tot += a[i];
        for (register int i = 1; i <= n; ++i) {
            f[i] = f[que[h]] + a[i];
            while (h <= t && f[i] <= f[que[t]]) t--;
            que[++t] = i;
            while (h <= t && que[h] <= i - m - 1) h++;
        }
        ll ans = 0;
        for (register int i = n - m; i <= n; ++i) ans = std::max(ans, tot - f[i]);
        printf("%lld\n", ans);
        return 0;
    }
}
int main() {
#ifdef LOCAL
    freopen("textname.in", "r", stdin);
    freopen("textname.out", "w", stdout);
#endif
    shl::main();
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/shl-blog/p/11520354.html