【CODEFORCES】 C. George and Job

C. George and Job
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn't have enough money, so George was going to work as a programmer. Now he faced the following problem at the work.

Given a sequence of n integers p1, p2, ..., pn. You are to choose k pairs of integers:

[l1, r1], [l2, r2], ..., [lk, rk] (1 ≤ l1 ≤ r1 < l2 ≤ r2 < ... < lk ≤ rk ≤ nri - li + 1 = m), 

in such a way that the value of sum  is maximal possible. Help George to cope with the task.

Input

The first line contains three integers nm and k (1 ≤ (m × k) ≤ n ≤ 5000). The second line contains n integers p1, p2, ..., pn (0 ≤ pi ≤ 109).

Output

Print an integer in a single line — the maximum possible value of sum.

Sample test(s)
input
5 2 1
1 2 3 4 5
output
9
input
7 1 3
2 10 7 18 5 33 0
output
61


题解:这道题一开始想复杂了....

  题意即为在一个数列中,找到k个长度为m的串,使其和最大,且两个串不相交。

  设d[i][j]为以i结尾,其后还能有j个长度为m的串此时和的最大值,那么d[i][j]=max(d[i-1][j],d[i-m][j+1]+sum[..]) (sum为i-m+1到i的和)

  d[i-1][j]是不取第i个的情况,而d[i-m][j+1]+sum[..]为取第i个的情况。

  答案为d[n][0]。

  注意j要从k-1开始推。

#include <iostream>
#include <cstdio>

using namespace std;

int n,m,k,a[5005];
long long sum[5005],d[5005][5005];

int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]);
    for (int i=1;i<=n;i++)
        sum[i]=sum[i-1]+a[i];
    for (int i=m;i<=n;i++)
        for (int j=k-1;j>=0;j--)
    d[i][j]=max(d[i-1][j],d[i-m][j+1]+sum[i]-sum[i-m]);
    cout<<d[n][0]<<endl;
    return 0;
}




你可能感兴趣的:(动态规划,codeforces)