Educational Codeforces Round 69 D. Yet Another Subarray Problem

Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem

Topic Link

Meaning of the questions:

Seeking \ (\ sum_ {i = l } ^ {r} -k \ frac {r-l + 1} {m} \ rceil \ lceil \) minimum, \ (n-\ Leq. 3. 5 * 10 ^, m \ Leq 10 \) .

Ideas:

Since \ (m \) is small, then for a period interval, the middle portion may be divided into a plurality of length \ (m \) interval, the length of the ends are not more than \ (m \) .
So order \ (dp (i) \) represented by \ (I \) is the minimum value of the end sections, then the situation is as section end point, which is another case as an intermediate period, to transfer Points .
code show as below:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 3e5 + 5;
int n, m, k;
ll a[N], sum[N];
ll dp[N];
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> n >> m >> k;
    for(int i = 1; i <= n; i++) cin >> a[i], sum[i] = sum[i - 1] + a[i];
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= min(m, i); j++) {
            dp[i] = max(dp[i], sum[i] - sum[i - j] - k);
        }
        if(i >= m) dp[i] = max(dp[i], dp[i - m] + sum[i] - sum[i - m] - k);
    }
    ll ans = 0;
    for(int i = 1; i <= n; i++) ans = max(ans, dp[i]);
    cout << ans;
    return 0;
}

Another algorithm, since the remainder is small, so to (I \) \ % \ (m \) values of classification, and maintains a prefix for each minimum remainder.
The answer is updated with the current prefix minus the maintenance of a minimum on the line.

Guess you like

Origin www.cnblogs.com/heyuhhh/p/11306747.html