您的位置:首页 > 其它

hdoj 1024 Max Sum Plus Plus 【动态规划】

2015-11-10 19:49 302 查看

Max Sum Plus Plus

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 21508 Accepted Submission(s): 7202



[align=left]Problem Description[/align]
Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more difficult problem.

Given a consecutive number sequence S1, S2, S3, S4 ... Sx, ... Sn (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ Sx ≤ 32767). We define
a function sum(i, j) = Si + ... + Sj (1 ≤ i ≤ j ≤ n).

Now given an integer m (m > 0), your task is to find m pairs of i and j which make sum(i1, j1) + sum(i2, j2) + sum(i3, j3) + ... + sum(im,
jm) maximal (ix ≤ iy ≤ jx or ix ≤ jy ≤ jx is not allowed).

But I`m lazy, I don't want to write a special-judge module, so you don't have to output m pairs of i and j, just output the maximal summation of sum(ix, jx)(1 ≤ x ≤ m) instead. ^_^

[align=left]Input[/align]
Each test case will begin with two integers m and n, followed by n integers S1, S2, S3 ... Sn.

Process to the end of file.

[align=left]Output[/align]
Output the maximal summation described above in one line.

[align=left]Sample Input[/align]

1 3 1 2 3
2 6 -1 4 -2 3 -2 3分析:求子段最大和。代码:[code]#include<stdio.h>
#include<string.h>
#include<algorithm>
#define Min  0x80000000
using namespace std;
const int  N= 1e6+10;
int pp
,bb
,dp
;
int main()
{
int n,m,i,j,k,max;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=m;i++)
scanf("%d",&pp[i]);
for(i=0;i<=m;i++)
dp[i]=0,bb[i]=0;
for(i=1;i<=n;i++)
{
max=Min;
for(j=i;j<=m;j++)
{
if(dp[j-1]>bb[j-1])
dp[j]=dp[j-1]+pp[j];
else dp[j]=bb[j-1]+pp[j];
bb[j-1]=max;
if(max<dp[j])
max=dp[j];
}
bb[j-1]=max;
}
printf("%d\n",max);
}
return 0;
}

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: