Codeforces round #267 div2 C George and job -- DP

Source: Internet
Author: User

The number of consecutive small sequences with a length of N is divided into K m-long continuous small sequences.

Solution: Apparently DP.

Definition: DP [I] [J] divides the first I elements into j m ends, and I is the largest sum at the end of J.

So there are: DP [I] [J] = max (DP [I-1] [J], DP [I-m] [J-1] + sum [I]-sum [I-m])

5000*5000 space ..

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#define lll __int64using namespace std;lll dp[5002][5001];lll a[5002],sum[5003];int main(){    int n,m,k;    int i,j;    while(scanf("%d%d%d",&n,&m,&k)!=EOF)    {        sum[0] = 0;        for(i=1;i<=n;i++)        {            scanf("%I64d",&a[i]);            sum[i] = sum[i-1]+a[i];        }        for(i=0;i<=n;i++)            for(j=0;j<=k;j++)                dp[i][j] = 0;        for(i=m;i<=n;i++)        {            dp[i][0] = max(dp[i][0],dp[i-1][0]);            for(j=1;j<=k;j++)            {                dp[i][j] = max(dp[i][j],dp[i-1][j]);                dp[i][j] = max(dp[i][j],dp[i-m][j-1]+sum[i]-sum[i-m]);            }        }        cout<<dp[n][k]<<endl;    }    return 0;}
View code

 

Codeforces round #267 div2 C George and job -- DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.