hdu1244——Max Sum Plus Plus Plus

Max Sum Plus Plus Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1339    Accepted Submission(s): 671


Problem Description
给定一个由n个正整数组成的整数序列

a1 a2 a3 ... an

求按先后次序在其中取m段长度分别为l1、l2、l3...lm的不交叠的连续整数的和的最大值。
 

Input
第一行是一个整数n(0 ≤ n ≤ 1000),n = 0表示输入结束
第二行的第一个数是m(1 ≤ m ≤ 20),
第二行接下来有m个整数l1,l2...lm。
第三行是n个整数a1, a2, a2 ... an.
 

Output
输出m段整数和的最大值。
 

Sample Input
 
   
3 2 1 1 1 2 3 4 2 1 2 1 2 3 5 0
 

Sample Output
 
   
5 10
 

Author
JGShining(极光炫影)
 

Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:   1024  1074  1203  1227  1224 
 

Statistic | Submit | Discuss | Note


dp[i][j]表示把前i个数分成j份,得到的和的最大值,则 dp[i][j] = max(dp[i - 1][j], dp[i - seg[j][j - 1] + sum);


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

const int N = 1010;
const int M = 22;

int sum[N];
int dp[N][M];
int arr[N], seg[M];
int main()
{
	int n, m;
	while(~scanf("%d", &n), n)
	{
		sum[0] = 0;
		scanf("%d", &m);
		for (int i = 1; i <= m; ++i)
		{
			scanf("%d", &seg[i]);
		}
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d", &arr[i]);
			sum[i] = sum[i - 1] + arr[i];
		}
		memset (dp, 0, sizeof(dp));
		for (int i = 1; i <= n; ++i)
		{
			for (int j = 1; j <= m; ++j)
			{
				dp[i][j] = max(dp[i][j], dp[i - 1][j]);
				if (i > seg[j])
				{
					dp[i][j] = max(dp[i][j], dp[i - seg[j]][j - 1] + sum[i] - sum[i - seg[j]]);
				}
			}
		}
		printf("%d\n", dp[n][m]);
	}
	return 0;
}


你可能感兴趣的:(DP,hdu)