其他分享
首页 > 其他分享> > 蒟蒻君的刷题日记Day16(DP专题T5):CF467C George and Job

蒟蒻君的刷题日记Day16(DP专题T5):CF467C George and Job

作者:互联网

解题思路

很明显的 dp 题。

代码实现

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5005;
int a[N], s[N], q[N], dp[N][N];
signed main() {
    ios :: sync_with_stdio(0);
    int n, m, k;
    cin >> n >> m >> k;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        s[i] = s[i - 1] + a[i];
    }
    int res = 0;
    for (int j = 1; j <= k; ++j) {
        memset(q, 0, sizeof q);
        int head = 1, tail = 1;
        for (int i = m; i <= n; ++i) {
            if (head <= tail) {
                dp[i][j] = max(dp[i][j], dp[q[head]][j - 1] + s[i] - s[i - m]);
            }
            while (head <= tail && dp[i - m + 1][j - 1] > dp[q[tail]][j - 1]) {
                --tail;
            }
            q[++tail] = i - m + 1;
            res = max(res, dp[i][k]);
        }
    }
    cout << res << '\n';
    return 0;
}

标签:head,CF467C,int,res,T5,tail,Job,序列,dp
来源: https://blog.csdn.net/yueyuedog/article/details/122877428