Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 42478 Accepted Submission(s): 15335
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. ^_^
Process to the end of file.
8
Huge input, scanf and dynamic programming is recommended.
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#pragma GCC optimize(2)
//#include<bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include<sstream>
#include<iterator>
#include<cstring>
#include<string>
#include<cstdio>
#include<cctype>
#include<vector>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std; typedef double dou;
typedef long long ll;
#define pai acos(-1.0)
#define M 1000005
#define inf 0x3f3f3f3f
#define mod 1e18
#define left k<<1
#define right k<<1|1
#define W(a) while(a)
#define ms(a,b) memset(a,b,sizeof(a)) int n, m, ans;
int num[M], head[M], dp[M]; int main() {
ios::sync_with_stdio(false);
while (cin >> m >> n) {
ms(head, ), ms(dp, );
for (int i = ; i <= n; i++) {
cin >> num[i];
}
for (int i = ; i <= m; i++) {
ans = -inf;
for (int j = i; j <= n; j++) {
dp[j] = max(dp[j - ], head[j - ]) + num[j];
head[j - ] = ans;//此时的ans是i-1段时候的最大值
ans = max(dp[j], ans);//此时的ans是i段的时候的最大值
}
}
cout << ans << endl;
}
return ;
}