HDU 1024:max Sum plus Plus (DP)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=1024Max Sum plus Plus

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) total submission (s): 24675 acce pted Submission (s): 8478

problem DescriptionNow I Think you has got an AC in IGNATIUS.L ' s "Max Sum" problem. To is a brave acmer, we always challenge ourselves to more difficult problems. Now you is faced with a more difficult problem.
Given a consecutive number sequence S1, S2, S3, S4 ... Sx... Sn (1≤x≤n≤1,000,000, -32768≤sx≤32767). We define a function sum (i, j) = Si + ... + Sj (1≤i≤j≤n).
Now given a integer m (M > 0), your task is to find m pairs of I and J which make sum (I1, J1) + sum (i2, J2) + SUM ( i3, J3) + ... + sum (IM, JM) maximal (IX≤IY≤JX or IX≤JY≤JX is not allowed).
But i ' m lazy, I don't want to the write a Special-judge module, so you don ' t has to output m pairs of I and J, just OUTPU T the maximal summation of sum (ix, JX) (1≤x≤m) instead. ^_^  InputEach test case would begin with a integers m and n, followed by n integers S1, S2, S3 ... Sn. Process to the end of file. OutputOutput The maximal summation described above in one line.  Sample Input1 3 1 2 6-1 4-2 3-2 3  Sample Output6 8 HintHuge Input, scanf and dynamic programming is recommended. Test Instructions: Divides a string of numbers into M-series, seeking the maximum possible sum.
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <iostream>5 using namespacestd;6 #defineN 10000057 #defineINF-10000000008 intNum[n],dp[n],mmax[n];9 /*Ten status Dp[i][j] One The number of the first J can be divided into the maximum of the I group A Dp[i][j]=max (Dp[i][j-1]+num[j], Max (Dp[i-1][k]) +num[j]),0<k<j; - Dp[i][j-1] is to add num[j] to the front of a group, - Max (Dp[i-1][k]) +num[j] is to re-divide the num[j into another group, the Max (Dp[i-1][k]) is the maximum value that is divided into I-1 groups - open a Mmax array each time can be saved into I-1 groups when using the first j-1 number of times the maximum value - time Complexity O (MN) - */ + intMain () - { +     intn,m; A      while(~SCANF ("%d%d",&m,&N)) { at          for(intI=1; i<=n;i++){ -scanf"%d", num+i); -         } -Memset (DP,0,sizeof(DP)); -memset (Mmax,0,sizeof(Mmax)); -         intans; in          for(intI=1; i<=m;i++){ -ans=INF; to              for(intj=i;j<=n;j++){ +Dp[j]=max (dp[j-1],mmax[j-1])+Num[j]; -mmax[j-1]=ans; theans=Max (Dp[j],ans); *             } $         }Panax Notoginsengcout<<ans<<Endl; -     } the     return 0; +}

2016-06-21

HDU 1024:max Sum plus Plus (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.