【CodeForces - 467C】George and Job(dp,思维)
題干:
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 5Output
9Input
7 1 3 2 10 7 18 5 33 0Output
61題目大意:
? 給定n,m,k。求k個(gè)子段,每一段的長度是m,且每一段均要求不相交。求選中段的最大和。
解題報(bào)告:
直接dp[i][j]代表前i個(gè)數(shù),分成j個(gè)子段,的最大和,然后轉(zhuǎn)移即可。
也可以定義成dp[i][j]代表以第i個(gè)數(shù)為結(jié)尾,分成j個(gè)子段的最大和,這樣的話需要枚舉上一個(gè)斷點(diǎn),不過可以用一個(gè)數(shù)組預(yù)處理前綴最大值,所以復(fù)雜度是一樣的,都可解。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; ll a[MAX],sum[MAX],dp[5005][5005],n,m,k; int main() {cin>>n>>m>>k;for(int i = 1; i<=n; i++) scanf("%lld",a+i);for(int i = 1; i<=n; i++) sum[i] = sum[i-1] + a[i];for(int i = m; i<=n; i++) {for(int j = 1; j<=k; j++) {dp[i][j] = max(dp[i-1][j],dp[i-m][j-1] + sum[i]-sum[i-m]);}}printf("%lld\n",dp[n][k]);return 0 ; }?
總結(jié)
以上是生活随笔為你收集整理的【CodeForces - 467C】George and Job(dp,思维)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 民生信用卡优惠活动 7天精彩特权等着你
- 下一篇: 【牛客 - 185B】路径数量(离散数学