kuangbin 基础DP1
A. Max Sum Plus Plus
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12589 Accepted Submission(s): 4146
Problem Description
Now 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 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 an 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 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(ix, jx)(1 ≤ x ≤ m) instead. _
Input
Each test case will begin with two integers m and n, followed by n integers S1, S2, S3 … Sn.
Process to the end of file.
Output
Output the maximal summation described above in one line.
Sample Input
1 3 1 2 3
2 6 -1 4 -2 3 -2 3
Sample Output
6
8
Hint
Huge input, scanf and dynamic programming is recommended.
Author
JGShining(極光炫影)
題意:對應上面第二個案例,先輸入一個數代表組數,然后輸入多個數字,然后把這些數不能更改順序的分成開頭輸入的數(可以有數不納入任何一組內)得到最大值。比如第二列分為 6 -1 4 ------------------ 3 -2 3 兩組之和為8
思路:這是一道dp題目,dp[i][j]記錄著到第j個數為止把前面的數分為i組,并且第j個數要取得,所得到的最大值。
那么狀態轉移就是dp[i][j]=max(dp[i][j-1]+a[j],dp[i-1][i-1…j-1]+a[j])(其中后面指的是如果第j個數要自己獨立成一組,那么就枚舉組數為i-1的各種情況)
注意的是,為了防止空間溢出,就用2個滾動數組,pre[]記錄著i-1的情況其中,pre[j]等價于dp[i-1]k的最大值
總結
以上是生活随笔為你收集整理的kuangbin 基础DP1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 致驱动工程师的一封信
- 下一篇: 我等这个含蓄的技术男当上了CEO