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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

kuangbin 基础DP1

發布時間:2023/12/20 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的最大值

#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> #include<string> typedef long long ll; ll dp[1001000]; ll pre[1001000]; int a[1001000]; using namespace std;int main() {int n,m;while(cin>>m>>n){for(int i=1;i<=n;i++)scanf("%d",&a[i]);ll mx=-999999999;memset(dp,0,sizeof(dp));memset(pre,0,sizeof(pre));for(int i=1;i<=m;i++){mx=-999999999;for(int j=i;j<=n;j++){dp[j]=max(dp[j-1],pre[j-1])+a[j];pre[j-1]=mx;mx=max(dp[j],mx);}}printf("%lld\n",mx);}}

總結

以上是生活随笔為你收集整理的kuangbin 基础DP1的全部內容,希望文章能夠幫你解決所遇到的問題。

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