标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23976 Accepted Submission(s):
8199
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAX = 1e6 + 10; const int MIN = -1e8; int curr[MAX], pre[MAX], a[MAX]; int max_sum, n, m; int main() { while(cin >> m >> n) { for(int i = 1; i <= n; i++) cin >> a[i]; memset(curr,0,sizeof(curr)); memset(pre,0,sizeof(pre)); int j = 0; for(int i = 1; i <= m; i++) { max_sum = MIN; for(j = i; j <= n; j++) { curr[j] = max(curr[j - 1], pre[j - 1]) + a[j]; /* pre[j-1]表示的是上一个状态中i...j-1的最大值, max_sum更新之后表示的i...j的最大值,所以不能写反了 */ pre[j - 1] = max_sum; max_sum = max(max_sum, curr[j]); } //pre[j-1]中始终保持着前一个状态的最大值,这个很重要 pre[j - 1] = max_sum; } cout << max_sum << endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/lyf-acm/p/5438950.html