Max sum plus
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 16843 accepted submission (s): 5539
Problem descriptionnow I think you have got an AC in Ignatius. l's "Max sum" problem. to be a brave acmer, we always challenge ourselves to more difficult problems. now you are faced with a more difficult problem.
Given a consecutive number sequence s
1, S
2, S
3, S
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 an integer m (M> 0), your task is to find m pairs of I and j which make sum (I
1, J
1) + Sum (I
2, J
2) + Sum (I
3, J
3) +... + Sum (I
M, J
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 write a special-Judge module, so you don't have to output m pairs of I and J, just output the maximal summation of sum (I
X, J
X) (1 ≤ x ≤ m) instead. ^_^
Inputeach test case will begin with two integers m and n, followed by N integers s
1, S
2, S
3... S
N.
Process to the end of file.
Outputoutput the maximal summation described abve in one line.
Sample Input
1 3 1 2 32 6 -1 4 -2 3 -2 3
Sample output
68HintHuge input, scanf and dynamic programming is recommended.
Authorjgshining (Aurora shadow)
Recommendwe have carefully selected several similar problems for you: 1074 1081 1160 1069 1058 question: give you a sequence of no more than 1e6 length. You need to select M segments that do not overlap from the sequence. So that the sum of the M segments is the largest among all M segments. Idea: This question is an extension of the maximum continuous sum. The status settings are clever. DP [I] [k] indicates that K segments are selected from the sequence of the first I. The last section ends with the largest sum of ARR [I. It seems clever that a restriction condition is added and ended with arr [I] so that the recurrence can be performed. DP [I] [k] = max (DP [I-1] [K], DP [J] [k-1]) + arr [I]. J <I. DP [J] [k-1] is the maximum value of DP [1] [k-1]... DP [I-1] [k-1. M is not very big. In fact, it is not big. Otherwise, this question is not enough time. You can scroll through the process when calculating the DP. When calculating DP [J] [k-1], you can also change the Edge Calculation to save the one-dimensional cycle. For details, see the code:
#include<algorithm>#include<iostream>#include<string.h>#include<stdio.h>using namespace std;const int INF=0x3f3f3f3f;const int maxn=1000010;typedef long long ll;ll dp[maxn],tp,tt,ans;int arr[maxn];int main(){ int n,m,i,k; while(~scanf("%d%d",&m,&n)) { for(i=1;i<=n;i++) { scanf("%d",&arr[i]); dp[i]=0; } for(k=1;k<=m;k++) { tp=dp[k-1]; for(i=k;i<=n;i++) { tt=dp[i]; dp[i]=max(dp[i-1],tp)+arr[i]; tp=max(tp,tt); } } ans=dp[m]; for(i=m;i<=n;i++) ans=max(ans,dp[i]); printf("%I64d\n",ans); } return 0;}
HDU 1024 Max sum plus (DP & MAX. Continuous and enhanced Edition)