CodeForces - 467C George and Job(二维dp)

题目链接:点击查看

题目大意:给出n个数,现在要从中选取k段长度为m的不相交的子数列,问如何选择能使得覆盖的数之和最大

题目分析:读完题之和就感觉是个dp题,但不会呀,去看了题解才会的。。

因为要选取某一段的子数列之和,所以在输入的时候维护好前缀和,dp[ i ][ j ] 为到第 i 个数为止,选了 j 段后的最大值,则转移方程为:

dp[ i ][ j ] = max ( dp[ i - 1 ][ j ] , dp[ i-m ][ j-1 ] + sum[ i ] - sum [ i-m ] )

最后dp[ n ][ k ]就是答案了

代码:

#include<iostream>
#include<cstdio> 
#include<string>
#include<ctime>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
#include<unordered_map>
using namespace std;
 
typedef long long LL;
 
const LL inf=0x3f3f3f3f3f3f3f3f;
 
const int N=5e3+100;

LL sum[N],dp[N][N];

int main()
{
//	freopen("input.txt","r",stdin);
//	ios::sync_with_stdio(false);
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++)
	{
		int num;
		scanf("%d",&num);
		sum[i]=sum[i-1]+num;
	}
 	for(int i=1;i<=n;i++)
 		for(int j=1;j<=k;j++)
 			if(i>=m)
 				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;
}
发布了558 篇原创文章 · 获赞 16 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_45458915/article/details/104077381
今日推荐