codeforces D. Yet Another Subarray Problem(DP)

 

 Topic links:

Question is intended: to a length of an array of n and m, k, and the maximum value.

Solution: Consider dp practice, dp [i] [j] for the i-th representative point right, if the value of the maximum length of the remainder of m j.

Transfer equation: dp [i] [j] = dp [i-1] [j-1] + a [i] (j> 0)

     dp[i][j]=max(dp[i-1][m-1]+a[i]-k,a[i]-k)(j==0)

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=3e5+5;
ll dp[maxn][12];
ll a[maxn];
int main()
{
    ll n,m,k;
    cin>>n>>m>>k;
    for(int i = 1;i <= n;++i)
        scanf("%lld",&a[i]);
    for(int i = 0;i <= n;++i)
    for(int j = 0;j <= 10;++j)
        dp[i][j]=-1e18;
    dp[0][m-1]=0;
    ll ans=0;
    for(int i = 1;i <= n;++i)
    {
        for(int j = 0;j <= m-1;++j)
        {
            if(j==0)
            dp[i][j]=max(dp[i-1][m-1]+a[i]-k,a[i]-k);
            else
            dp[i][j]=dp[i-1][j-1]+a[i];
            ans=max(ans,dp[i][j]);
        }
    }
    printf("%lld\n",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/aaddvvaanntteezz/p/11233409.html