您的位置:首页 > 其它

Codeforces 467C George and Job(dp)

2014-09-26 20:14 363 查看
题目链接:Codeforces 467 George and Job

题目大意:给定一个长度为n的序列,从序列中选出k个不重叠且连续的m个数,要求和最大。

解题思路:dp[i][j]表示到第i个位置选了j个子序列。
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;

const int maxn = 5005;

int N, M, K;
ll arr[maxn], sum[maxn];
ll dp[maxn][maxn];

ll solve () {
ll ret = 0;
for (int i = M; i <= N; i++) {

ll tmp = sum[i] - sum[i-M];
for (int j = 1; j <= K; j++)
dp[i][j] = max(dp[i-1][j], dp[i-M][j-1] + tmp);
ret = max(ret, dp[i][K]);
}
return ret;
}

int main () {
scanf("%d%d%d", &N, &M, &K);
for (int i = 1; i <= N; i++) {
scanf("%lld", &arr[i]);
sum[i] = sum[i-1] + arr[i];
}
printf("%lld\n", solve());
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: