Educational Codeforces Round 69 (Rated for Div. 2) D

  • Title effect: a given sequence, and \ (m, K \) , seeking \ (\ sum_ {i = l } ^ {r} {a_i} -k \ left \ lceil \ frac {r-l + 1} { m} \ right \ rceil \) minimum (empty array can be selected)
  • Thinking: Since only 10 m maximum, we can enumerate each of length \ (1 to m-1 \) interval ( \ (\ left \ lceil \ {R & lt FRAC-L + 1} {m} \ right \ rceil 1 = \) ).
    \ (DP [I] \) representative of the \ (1 to I \) maximum, first find \ + 1 \) (i-m i to a maximum value of the sub-array, and then directly reducing go \ (K \) , with \ (dp [im] \) to update the current \ (dp [i] \)

\[dp[i] = max(dp[i],dp[i-m]+sum[i]-sum[i-m]-k);\]

  • Since \ (dp [im] \) represents the front to \ (IM \) maximum subarray ending, and should bring the entire transfer \ (m \) long section (taken to ensure continuous subarray)
#include<bits/stdc++.h>
#define ll long long 
#define FOR(i,n) for(int i =1; i <= n;++i ) 
#define FOR0(i,n) for(int i =0; i < n;++i )  
#define inf 0x3f3f3f3f
using namespace std; 


const int maxn = 3e5+10;
ll n,k,m;
ll a[maxn];
ll sum[maxn];
ll dp[maxn];
int main(){
    cin >> n >> m>> k;
    FOR(i,n){
        cin >> a[i];
        sum[i] = sum[i-1] + a[i] ;
    }
    ll ans = 0;
    for(int i=1;i<=n;++i){
        for(int j=1;j<=m&&j<=i;++j){    // 枚举 1 到 m 的区间
            dp[i] = max(dp[i],sum[i]-sum[i-j]);
        }
        dp[i] -= k;
        dp[i] = max(dp[i],0LL);
        if(i>m){                        // 前一个 来更新当前的
            dp[i] = max(dp[i],dp[i-m]+sum[i]-sum[i-m]-k);
        }
        ans = max(ans,dp[i]);
    }
    cout << ans <<endl;
    return 0;
}

Write a maximum field and really not too QAQ
Educational Codeforces Round 69 (Rated for Div. 2)

Guess you like

Origin www.cnblogs.com/xxrlz/p/11229780.html