ACM Dynamic programming Max M sub-segments and problems (in class)

Source: Internet
Author: User

Max Sum plus Plus

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 23976 Accepted Submission (s): 8199


Problem Descriptionnow I Think you have got a 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 S 1+ t 2+ t 3+ t 4... S x, ... S N(1≤x≤n≤1,000,000, -32768≤s x≤32767). We define a function sum (i, j) = S I+ ... + S J(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 (i 1J 1) + SUM (i 2J 2) + SUM (i 3J 3) + ... + sum (i mJ m) Maximal (i x≤i y≤j xOr I x≤j y≤j xis 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 output th e maximal summation of sum (i xJ x) (1≤x≤m) instead. ^_^

Inputeach test case would begin with a integers m and n, followed by n integers S 1+ t 2+ t 3... S N.
Process to the end of file.

Outputoutput the maximal summation described above in one line.

Sample INPUT1 3 1 2 32 6-1 4-2 3-2 3

Sample Output68  
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intMAX = 1e6 +Ten;Const intMIN =-1e8;intCurr[max], Pre[max], A[max];intmax_sum, N, M;intMain () { while(Cin >> M >>N) { for(inti =1; I <= N; i++) Cin>>A[i]; memset (Curr,0,sizeof(Curr)); memset (PRE,0,sizeof(pre)); intj =0;  for(inti =1; I <= m; i++) {max_sum=MIN;  for(j = i; J <= N; j + +) {Curr[j]= Max (Curr[j-1], Pre[j-1]) +A[j];/*Pre[j-1] Represents the maximum value of i...j-1 in the previous state, max_sum the maximum value of the I...J represented after the update, so it cannot be written back*/pre[j-1] =max_sum; Max_sum=Max (Max_sum, Curr[j]); }            //Pre[j-1] The maximum value of the previous state is always maintained, which is importantPre[j-1] =max_sum; } cout<< Max_sum <<Endl; }    return 0;}

ACM Dynamic programming Max M sub-segments and problems (in class)

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.