HDU-1024 Max Sum plus Plus (dp+ scrolling array optimization)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1024

Test instructions: Given n numbers, the maximum value of the M segment (no continuous between segments and segments, but continuous in a period)

For example:2 5 1-2 2 3-1 Five digits Select 2, choose 1 and 2 3 for each of the two paragraphs.

Puzzle: dp[i][j] Select I in the former J number, and then according to whether the number of J is independent into a paragraph, you can write

State transition equation:dp[i][j]=max (Dp[i][j-1]+num[j],max (dp[i-1][k)) +num[j])

Here Max (Dp[i-1][k]) stands for the maximum value of the I-1 segment, and then adds Num[j] as a separate paragraph.

But the problem does not give the range of M, it is possible to burst memory and explosion, you need to deal with.

For explosion-proof Memory: note dp[i][*] only and dp[i][*],dp[i-1][*], that is, the current state is only related to the previous state, you can use the scrolling array optimization (data).

For explosion protection: since Max (Dp[i-1][k] represents the maximum value of the i-1 segment, we can use an array pre to store the maximum value before

State transition equation: Dp[i][j]=max (dp[i][j-1]+num[j],pre[j-1]+num[j]) Find out what I'm doing, so

The final state transfer equation:dp[j]=max (Dp[j-1]+num[j],pre[j-1]+num[j])

1#include <iostream>2#include <algorithm>3 using namespacestd;4 5 Const intn=1000010;6 Const intinf=0x3f3f3f3f;7 intNum[n],pre[n],dp[n];8 9 intMain () {Ten     intn,m; One      while(SCANF ("%d%d", &m,&n)! =EOF) { A          for(intI=1; i<=n;i++) scanf ("%d", &num[i]), dp[i]=0, pre[i]=0; -          -         intMAX; thedp[0]=pre[0]=0; -          for(intI=1; i<=m;i++){ -max=-INF; -              for(intj=i;j<=n;j++) {//here starts with I, because I need to have at least I number to support I segment  +Dp[j]=max (dp[j-1]+num[j],pre[j-1]+num[j]); -pre[j-1]=MAX; +max=Max (max,dp[j]); A             } at         } -              -printf"%d\n", MAX);  -     }  -     return 0; -}

HDU-1024 Max Sum plus Plus (dp+ scrolling array optimization)

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.