A Mini locomotive (Dynamic planning 01)

Source: Internet
Author: User

 /*  Test Instructions: Select the number of 3 consecutive numbers   The interval of K to make their and maximum analysis:  dp[j][i]=max (Dp[j-k][i-1]+value[j],dp[j-1][i]);  Dp[j][i]: The maximum and value[j of I continuous intervals   values are selected from the J Number: the number in the J interval is a bit like the backpack, but the  */  #include <cstdio> #include <cstring> #include <iostream>using namespace Std;int dp[50005][4]; int main () {    int t;     scanf ("%d", &t);    while (t--)     {           int n;        scanf ("%d", &n);        int a[n+1],sum[n+1];        Memset (Dp,0,sizeof (DP));        memset (A,0,sizeof (a));        memset (sum , 0,sizeof (sum));         for (int i=1; i<=n; i++)         {    &NB Sp       scanf ("%d", &a[i]);            sum[i]=sum[i-1]+a[i];//prefix and, For the number of consecutive K        }     &NBSp     int k=0;        scanf ("%d", &k);        int value[n+1];  &NB Sp     memset (value,0,sizeof (value));        for (int i=1; i<=k; i++)       &NB Sp     value[i]=value[i-1]+a[i];        for (int i=k+1; i<=n; i++)             value[i]=sum[i]-sum[i-k];//consecutive K numbers and Value[i] represent the interval length k for the interval            for (int j=k; j<=n; j + +)             for (int i=1; i<=3; i++)                 Dp[j][i]=max (dp[j-k][i-1]+value[j],dp[j-1][i]);//Select the I interval from the J number, It is equivalent to the number of previous (J-k) selected (I-1) The basis of the interval (value[j]), if not selected is equivalent to the number of (j-1) Select I range                printf ("%d\n", Dp[n][3]);      }     return 0;}

A Mini locomotive (Dynamic planning)

Related Article

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.