EduCF-69 Yet Another Subarray Problem (dp)

Original title link

Question is intended: to give you a length n of the sequence, and a cost formula, and then seeking the most cost: [Sigma I = L R & lt A I - K R & lt - L + . 1 m [Sigma I = L R & lt A I - K R & lt - L + . 1 m ,
generally subsequence cost issues will certainly occur to dp, it is necessary to indicate what state by dp
This question is binding and the maximum continuous sequence, and the sequence lengths impact the cost
due to the influence of the length Therefore, the status information to be recorded not just the current position
dp [i] [j], i is the current position, j is other than size; while dp [i] [1] ~ dp [i] [m] also contains the All sequences at length the state of
our main considerations in turn transferred the neighboring state due to length minus the problem of k
to be more a minus k: j = 1, meaning that either take the current one, or is it a pre-i- m is 1 and the state transferred from
 When diminished k: j> 1, that means from the other intermediate state is i-1, j-1 directly to the state

 

Complete code:

#include <cstdio>
    #include <iostream>
    using namespace std;
    typedef long long ll;
    const int inf = 0x3f3f3f3f ;
    const int N = 3e5 + 5;
    ll a[N];
    //ll sum[N];
    ll dp[N][20];
    int main()
    {
        int n, m, k;
        cin >> n >> m >> k;
        a[0] = 0;
        for (int i = 1; i <= m; i ++)dp[0][i] = -inf;
        ll ans = 0;
        for (int i = 1; i <= n; i ++){
            cin>>a[i];
            for (int j = 1; j <= m; j ++){
                if (j == 1)dp[i][j] = max(a[i] - k, dp[i - 1][m] + a[i] - k);
                else dp[i][j] = dp[i - 1][j - 1] + a[i];
                ans = max(ans, dp[i][j]);
            }
        }
        cout<<ans<<endl;
     
        return 0;
    }

 

Guess you like

Origin www.cnblogs.com/Tianwell/p/11276956.html