日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Max Sum Plus Plus HDU - 1024(动态规划求最大M子段和)

發布時間:2023/12/4 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Max Sum Plus Plus HDU - 1024(动态规划求最大M子段和) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:

----最大M子段和問題
給定由 n個整數(可能為負整數)組成的序列以及一個正整數 m,要求確定序列的 m個不相交子段,使這m個子段的總和達到最大,求出最大和。

題目:

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 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 x or i x ≤ j y ≤ j x 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(i x, j x)(1 ≤ x ≤ m) instead. _

Input

Each 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.

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

思路:

很明顯,我們不能知道某一點是否該加入段中,加入到那一段中,不能用貪心寫,那么只能用動態規劃,看某點j是否需要加入i段中,或是新開一段作為新段的開頭。

AC代碼

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; typedef long long ll; const int M=1e6+10; const int inf=0x3f3f3f3f; int n,m,dp[M],f[M],s[M],ans; int main() {while(~scanf("%d%d",&n,&m)){for(int i=1; i<=m; i++)scanf("%d",&f[i]);memset(dp,0,sizeof(dp));memset(s,0,sizeof(s));for(int i=1; i<=n; i++){ans=-inf;for(int j=i; j<=m; j++){dp[j]=max(dp[j-1]+f[j]/*f[j]屬于第i段*/,s[j-1]+f[j]/*f[j]不屬于第i段為新的段加上前i-1段,看是否較大*/);/*看該點是否為新段的開始*/s[j-1]=ans;/*上一段(i-1)到j-1點的最大值*/ans=max(dp[j],ans);/*在第i段第j點的最大值,為當前最大值*/}}printf("%d\n",ans);}return 0; }

總結

以上是生活随笔為你收集整理的Max Sum Plus Plus HDU - 1024(动态规划求最大M子段和)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。