HDU 1244 Max sum plus

Source: Internet
Author: User
Tags define local

Although this question looks like HDU 1024 Max sum plus, it seems easier than 1024.

Previously wa several times, because I started to write DP [22] [maxn] as DP [maxn] [22], orz

It seems that arrays out of bounds may not necessarily cause program crash or return an error.

 

DP [I] [J] indicates the maximum number of J numbers that constitute the first I segment.

State transition equation:

DP [I] [J] = max {DP [I] [J-1], DP [I-1] [J-len [I] + sum [J]-sum [J-len [I]}

Corresponding to not take the I number and take the J number and the adjacent total Len [I] Number respectively as the I segment plus the previous number constitute the maximum value of the I-1 segment

 

 1 //#define LOCAL 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6  7 const int maxn = 1000 + 10; 8 int a[maxn], sum[maxn], len[22], dp[22][maxn]; 9 10 int main(void)11 {12     #ifdef LOCAL13         freopen("1244in.txt", "r", stdin);14     #endif15 16     int n, m;17     while(scanf("%d", &n) == 1 && n)18     {19         scanf("%d", &m);20         for(int i = 1; i <= m; ++i)21             scanf("%d", &len[i]);22         for(int i = 1; i <= n; ++i)23             scanf("%d", &a[i]);24         sum[0] = 0;25         for(int i = 1; i <= n; ++i)26             sum[i] = sum[i - 1] + a[i];27         memset(dp, 0, sizeof(dp));28         for(int i  = 1; i <= m; ++i)29         {30             for(int j = 1; j < len[i]; ++j)31                 dp[i][j] = dp[i][j-1];32             for(int j = len[i]; j <= n; ++j)33                 dp[i][j] = max(dp[i][j-1], dp[i-1][j-len[i]] + sum[j] - sum[j-len[i]]);34         }35         printf("%d\n", dp[m][n]);36     }37     return 0;38 }
Code Jun

 

HDU 1244 Max sum plus

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.