最大子数组问题--分治法的思想

国外经典教材《算法导论》P67页提到了最大子数组的问题,书中给出两种解答方法,分别是暴力求解的方法,分治策略的求解方法。在杭电 ACM OJ 中也有一条类似的题目,连接如下:http://acm.hdu.edu.cn/showproblem.php?pid=1003

Max Sum

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


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
 

Author
Ignatius.L



题目分析:若用暴力求解的方法去做,时间复杂度会是O(n^2),会出现时间超限的问题。暴力求解代码如下:
#include<stdio.h>
int main()
{
int T;
int n;
int i;
scanf("%d",&T);
for(i=0;i<T;i++){
scanf("%d",&n);
int *p = new int [n];
int j;
for(j=0;j<n;j++){
scanf("%d",&p[j]);
}


int best;
int sum;
int first;
int end;
int k;
best = p[0];
first = end = 0;
//关键求解过程
for(j=0;j<n;j++){
sum = 0;
for(k=j;k<n;k++){
sum += p[k];
if(best<sum){
best = sum;
first = j;
end = k;
}
}
}
printf("Case %d:\n",i+1);
printf("%d %d %d\n",best,first+1,end+1);
delete []p;
}
return 0;
}


分治策略思想:
1.把问题尽可能划分为规模相等的子问题。
2.递归求解子问题
3.合并子问题的解得到原问题的解
#include<stdio.h>
struct node{
int first;
int end;
int summax;
node(int a,int b,int c):first(a),end(b),summax(c){}
};
node maxsum(int *p,int x,int y)
{
if(y-x==1||y==x){
return node(x+1,x+1,p[x]); //只有一个数
}
int mid = (x+y)/2;
//分治第一步:划分[x,mid)和[mid,y)
node maxsl = maxsum(p,x,mid);
node maxsr = maxsum(p,mid,y);
//分治第二步:递归求解
node maxs = maxsl.summax>=maxsr.summax?maxsl:maxsr;
int L,R,sum,i,a=mid-1,b=mid;
L = p[mid-1];
sum = 0;
//分治第三步:合并(1)——从分界点开始往左求最大连续和L
for(i=mid-1;i>=x;i--){
sum+=p[i];
if(sum>=L){
L = sum;
a = i;

}
R = p[mid];
sum = 0;
//分治第三步:合并(2)——从分界点开始往右求最大连续和R
for(i=mid;i<y;i++){
sum+=p[i];
if(sum>R){
R = sum;
b = i;
}
}
if(maxs.summax>L+R){
return maxs;
}
else{
return node(a+1,b+1,L+R);
}
}
int main()
{
int T,i;
scanf("%d",&T);
for(i=0;i<T;i++){
int n,j;
int *p;
scanf("%d",&n);
p = new int [n];
for(j=0;j<n;j++){
scanf("%d",&p[j]);
}
node maxs = maxsum(p,0,n);
printf("Case %d:\n",i+1);
printf("%d %d %d\n",maxs.summax,maxs.first,maxs.end);
if(i!=T-1){
printf("\n");
}
}
return 0;
}

另外网上对于该题还有动态规划(dp)等的求解方法,链接如下:http://blog.csdn.net/hcbbt/article/details/10454947

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值