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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

George and Job(动态规划)

發布時間:2023/12/15 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 George and Job(动态规划) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn’t have enough money, so George was going to work as a programmer. Now he faced the following problem at the work.

Given a sequence of n integers p1,?p2,?…,?pn. You are to choose k pairs of integers:

[l1,?r1],?[l2,?r2],?…,?[lk,?rk] (1?≤?l1?≤?r1?<?l2?≤?r2?<?…?<?lk?≤?rk?≤?n; ri?-?li?+?1?=?m),?
in such a way that the value of sum is maximal possible. Help George to cope with the task.

Input
The first line contains three integers n, m and k (1?≤?(m?×?k)?≤?n?≤?5000). The second line contains n integers p1,?p2,?…,?pn (0?≤?pi?≤?109).

Output
Print an integer in a single line — the maximum possible value of sum.

Examples
Input
5 2 1
1 2 3 4 5
Output
9
Input
7 1 3
2 10 7 18 5 33 0
Output
61
題意:從n個數中選出k段長度為m的連續數組,使得這k段加和最大。
所選取的段數可以是連續的,也可以不是連續的。我們使dp[i][j]代表著從前i個數字中選取j段的最大值。對于dp[i][j]來說,它可以是前i-1個數選取j段,也可以是前i-m段選取j-1段之后,再選取一段。
狀態轉移方程:dp[i][j]=max(dp[i-1][j],dp[i-m][j-1]+cnt);//cnt為從i-m到i的數字和。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=5e3+100; ll a[maxx],sum[maxx]; ll dp[maxx][maxx]; int n,m,k;int main() {scanf("%d%d%d",&n,&m,&k);for(int i=1;i<=n;i++) scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i];int x=m-1;ll cnt;for(int i=m;i<=n;i++){cnt=sum[i]-sum[i-x-1];for(int j=1;j<=k;j++){dp[i][j]=max(dp[i-1][j],dp[i-m][j-1]+cnt);}}printf("%lld\n",dp[n][k]); }

努力加油a啊,(o)/~

總結

以上是生活随笔為你收集整理的George and Job(动态规划)的全部內容,希望文章能夠幫你解決所遇到的問題。

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