427人阅读 评论(0)

# Max Sum Plus Plus

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

Problem Description
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 Im 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. ^_^

Input
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.

Output
Output the maximal summation described above in one line.

Sample Input
1 3 1 2 3
2 6 -1 4 -2 3 -2 3

Sample Output
6
8

Hint
Huge input, scanf and dynamic programming is recommended.

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int mod=1e9+7;

int dp[maxn],map[maxn],mmax[maxn];
//dp[i]代表当前选取i个数中，从1到i的和是多少，mmax储存前一行的数值

int main()
{
int n,m,i,j,k;
while(cin>>m>>n)
{
for(i=1;i<=n;i++)
cin>>map[i];

mset(dp,0);
mset(mmax,0);
int maxx;
for(i=1;i<=m;i++)   //枚举m次选择
{
maxx=-inf;      //初始为最小
for(j=i;j<=n;j++)   //枚举每一次选择
{
dp[j]=max(dp[j-1]+map[j],mmax[j-1]+map[j]);  //当前值取前j-1个数的和跟前一行的和的最大值
mmax[j-1]=maxx;  //储存前一行
maxx=max(dp[j],maxx);  //储存最大值
}
}
cout<<maxx<<endl;
}
return 0;
}`

1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：34696次
• 积分：1586
• 等级：
• 排名：千里之外
• 原创：127篇
• 转载：1篇
• 译文：0篇
• 评论：13条
联系方式
欢迎谈论交流：1245985209
博客专栏
 CodeForce题解 文章：26篇 阅读：8114
 ACM的进阶之路 文章：68篇 阅读：20950
文章分类
最新评论