codeforces 467-C. George and Job(前缀和+dp)
来源:互联网 发布:观察者模式js 编辑:程序博客网 时间:2024/06/05 10:45
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的不相交的区间,使这些区间的和最大。
解题思路:
状态表示:
dp[i][j]表示前i个数找出j段长度为m的最大区间和。
状态转移方程:
dp[i][j]=max(d[i-1][j],d[i-m][j-1]+sum[i]-sum[i-m]);
其中sum[i]表示前i个数的和。
#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define maxn 5005typedef long long LL;LL s[maxn],sum[maxn],dp[maxn][maxn];int main(){ LL n,m,k; while(~scanf("%lld%lld%lld",&n,&m,&k)) { memset(sum,0,sizeof(sum)); memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) { scanf("%lld",&s[i]); sum[i]=sum[i-1]+s[i]; } for(int i=1;i<=n;i++) for(int j=1;j<=k;j++) dp[i][j]=i>=m?max(dp[i-1][j],dp[i-m][j-1]+sum[i]-sum[i-m]):dp[i-1][j]; printf("%lld\n",dp[n][k]); } return 0;}
- codeforces 467-C. George and Job(前缀和+dp)
- codeforces 467C George and Job dp
- Codeforces 467C George and Job(dp)
- Codeforces 467C George and Job(dp)
- Codeforces 467C George and Job(dp)
- codeforces-467C-George and Job【dp】
- [CodeForces 467C]George and Job[DP]
- Codeforces 467C George and Job DP
- Codeforces 467C George and Job(dp)
- Codeforces 467C George and Job【dp】好题~
- codeforces #267 C George and Job(DP)
- Codeforces 467C. George and Job
- Codeforces 467C George and Job
- codeforces 467C - George and Job
- Codeforces 467 C. George and Job
- CodeForces - 467C George and Job
- CodeForces 467C George and Job
- 【DP】 467C George and Job
- mybatis与spring整合(方式一)
- java截屏代码
- java webservice 开发方法(REST方式)---续(父类写法及使用)
- hibernate 老版本在存dateTime时,报value为空
- vim 字符编码设置 及 修改编码
- codeforces 467-C. George and Job(前缀和+dp)
- 简单介绍结构体重载
- Maven 国内镜像很慢解决的方法
- PCAP文件结构
- deep learning 学习资料
- spring DelegatingFilterProxy 过滤器 的原理及运用
- 把apk提交腾讯应用市场的时候审核失败—未通过原因:拉取应用审核信息失败
- 软件测试学习笔记(1):软件测试概要
- JavaScript之Array方法不完全总结