Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 156116 Accepted Submission(s): 36492
Problem Description
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
Sample Input
2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5
Sample Output
Case 1: 14 1 4 Case 2: 7 1 6
解题报告:求最长子序列的和,可以直接用O(n)动态规划或者是用O(n*logn)的分治,当然既然是分治专题,还是要把两种做法都写一遍。
首先是动态规划,就是从左向右统计更新,如果遇到使和小于0的数,就把和直接赋成为当前值,如果和大于0就继续加。两个操作都要更新下标。要考虑全为负数的情况,所以我添加了i == 0单独处理。
我的代码:
/********************************/
/*Problem: hdu1003 */
/*User: shinelin */
/*Memory: 1156K */
/*Time: 31 MS */
/*Language: G++ */
/********************************/
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cstring>
#include <string>
#include <list>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;
#define INF 0x7fffffff
#define LL long long
int T, l, u, sum, Max;
int n, x, k = 0;
int down, up;
void updateMax(int M)
{
if(M > Max)
{
Max = M;
down = l + 1;
up = u + 1;
}
}
int main()
{
scanf("%d", &T);
while(T --)
{
sum = Max = -INF;
l = u = 0;
scanf("%d", &n);
for (int i = 0; i < n; i ++)
{
scanf("%d", &x);
if(i == 0)
updateMax(x);
if(sum < 0)
{
sum = x;
l = u = i;
updateMax(sum);
}
else
{
sum += x;
u ++;
updateMax(sum);
}
}
if(k > 0) printf("\n");
printf("Case %d:\n", ++k);
printf("%d %d %d\n", Max, down, up);
}
return 0;
}
别人的代码 @Adorkable http://blog.csdn.net/u012431590/article/details/42499355
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <fstream>
#include <ctime>
#include <stack>
using namespace std;
const int MAXN = 100000 + 5;
int T, n, st, en, sta;
int arr[MAXN];
int main()
{
scanf("%d", &T);
for(int t = 1; t <= T; t++)
{
scanf("%d", &n);
int sum = 0, Max = 0;
st = sta = 0;
en = n - 1;
for(int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
sum += arr[i];
if(sum > Max)
{
Max = sum;
en = i;
st = sta;
}
else if(sum < 0)
{
sum = 0;
sta = i + 1;
}
}
printf("Case %d:\n", t);
printf("%d %d %d\n\n", Max, st+1, en+1);
}
return 0;
}
用分治也可以实现,求左半边的最大值和右半边的最大值,以及包含中间点的子串的最大和,三者比较,取最大值。关键在于如何记录下标,可以用一个结构体存储最大值及其对应的下标区间。
/********************************/
/*Problem: hdu1003 */
/*User: shinelin */
/*Memory: 2060K */
/*Time: 31 MS */
/*Language: G++ */
/********************************/
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cstring>
#include <string>
#include <list>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;
#define INF 0x7fffffff
#define LL long long
vector<int> v;
struct Node
{
int down;
int up;
int sum;
Node(int m, int d, int u):sum(m),down(d), up(u)
{
}
};
Node MaxSubSum(vector<int>& v, int first, int last)
{
if(first + 1 == last)
{
return Node(v[first], first, first);
}
int mid = (first + last) >> 1;
Node MaxleftSum = MaxSubSum(v, first, mid);
Node MaxrightSum = MaxSubSum(v, mid, last);
Node M = MaxleftSum.sum >= MaxrightSum.sum ? MaxleftSum : MaxrightSum;
//left
int x = mid - 1, y = mid;
int leftSum = 0, leftMaxSum = v[mid-1];
for (int i = mid - 1; i >= first; i --)
{
leftSum += v[i];
if(leftSum >= leftMaxSum)
{
leftMaxSum = leftSum;
x = i;
}
}
//right
int rightSum = 0, rightMaxSum = v[mid];
for (int i = mid; i < last; i ++)
{
rightSum += v[i];
if(rightSum > rightMaxSum)
{
rightMaxSum = rightSum;
y = i;
}
}
int midSum = leftMaxSum + rightMaxSum;
if(M.sum > midSum)
{
return M;
}
return Node(midSum, x, y);
}
int main()
{
int k = 0, T, n, x;
scanf("%d", &T);
while(T --)
{
v.clear();
scanf("%d", &n);
v.push_back(-1);
for (int i = 1; i <= n; i ++)
{
scanf("%d", &x);
v.push_back(x);
}
Node ans = MaxSubSum(v, 1, v.size());
if(k > 0) printf("\n");
printf("Case %d:\n", ++k);
printf("%d %d %d\n", ans.sum, ans.down, ans.up);
}
return 0;
}