标签:算法 acm codeforces algorithm hdu
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:
in such a way that the value of sum is maximal possible. Help George to cope with the task.
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).
Print an integer in a single line — the maximum possible value of sum.
5 2 1 1 2 3 4 5
9
7 1 3 2 10 7 18 5 33 0
61
if(i-m>=0) dp[i][j] = max(dp[i][j],max(dp[i-1][j],dp[i-m][j-1]+b[i]-b[i-m])) ;
else
<span style="white-space:pre"> </span>dp[i][j] = max(dp[i][j],dp[i-1][j]) ;其中b[i]存前i项a[i]的和。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; long long dp[5005][5005] ; long long a[65555] ; long long b[65555] ; int main() { int n,m,k ; cin>>n>>m>>k; int i,j; for(i=1;i<=n;i++) { cin>>a[i]; b[i] = b[i-1] + a[i] ; } for(i=1;i<=n;i++) { for(j=1;j<=k;j++) { if(i-m>=0) dp[i][j] = max(dp[i][j],max(dp[i-1][j], dp[i-m][j-1]+b[i]-b[i-m])) ; else dp[i][j] = max(dp[i][j],dp[i-1][j]) ; } } cout<<dp[n][k]<<endl; return 0 ; }
Codeforces Round #267 (Div. 2) C
标签:算法 acm codeforces algorithm hdu
原文地址:http://blog.csdn.net/notdeep__acm/article/details/39388427