01-复杂度1 最大子列和问题 (20 分)
本题旨在测试各种不同的算法在各种数据情况下的表现。各组测试数据特点如下:
数据1:与样例等价,测试基本正确性;
数据2:102个随机整数;
数据3:103个随机整数;
数据4:104个随机整数;
数据5:105个随机整数;
输入格式:
输入第1行给出正整数K (≤100000);第2行给出K个整数,其间以空格分隔。
输出格式:
在一行中输出最大子列和。如果序列中所有整数皆为负数,则输出0。
输入样例:
6
-2 11 -4 13 -5 -2
输出样例:
20
算法一:暴力,直接找左端点和右端点,找所有情况。
复杂度:O(N^3)。
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <ctime>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
#define maxn 100005
#define mod 7654321
int main()
{
int a[maxn];
int n,maxx=0;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<n;i++)//左端点
{
for(int j=i;j<n;j++)//右端点
{
int sum=0;
for(int k=i;k<=j;k++)
{
sum+=a[k];
}
if(maxx<sum)
maxx=sum;
}
}
cout<<maxx;
return 0;
}
运行结果:一万个数据就已经超时。
算法二:第三次的k循环完全没必要,从i到j其实每次就加了一项
eg: 0~1 0~2 0~3 …只比前一个区间的和多加了一项,完全没必要再从头加到尾
所以直接去掉k循环,将sum初始化放到第一层for循环里就行。。
复杂度:O(N^2)。 减少了一个数量级。。
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <ctime>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
#define maxn 100005
#define mod 7654321
int main()
{
int a[maxn];
int n,maxx=0;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<n;i++)//左端点
{
int sum=0;
for(int j=i;j<n;j++)//右端点
{
sum+=a[j];//从i到j其实每次就加了一项eg:0~1 0~2....只比钱一个区间的和多加了一项
if(maxx<sum)
maxx=sum;
}
}
cout<<maxx;
return 0;
}
运行结果:没有超时
算法三:有了O(N^2),就应该想到可以将其优化为<mark>O(NlogN)</mark>。
<mark>分而治之思想</mark>
<mark>分:复杂问题切成小块,分别处理; 治:最后再把结果合并起来。</mark>
<mark>求三数字的最大值:</mark>
解释:上面是下面的解释
if(a > b)
if(a > c) return a;
else return c;
else
if(b > c) return b;
else return c;
学习:
int Max3( int A, int B, int C )
{
return A > B ? A > C ? A : C : B > C ? B : C;
}
正式代码:不好写。求跨边界最大子列和时,必须从中间往左,从中间往右找对应的最大和,相加,因为:最大子列和要连续,不能断开。
递归出口:左右相遇。
求最大值:每次递归完成,都会去求左子列,右子列,以及跨边界的最大子列,三者的最大值,将其更新到MaxLeftSum 和MaxRightSum 中。
函数流程:先写递归退出条件,再写左右递归,再写每次递归需要求得跨边界的左子列和右子列最大值,再写返回三者的最大值。
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <ctime>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
#define maxn 100005
#define mod 7654321
/* 返回3个整数中的最大值 */
int Max3( int A, int B, int C );
/* 分治法求List[left]到List[right]的最大子列和 */
int DivideAndConquer( int List[], int left, int right );
int main()
{
int List[maxn];
int n,maxx=0;
cin>>n;
for(int i=0;i<n;i++)
cin>>List[i];
cout<<DivideAndConquer(List,0,n-1)<<endl;
return 0;
}
/* if(a > b) if(a > c) return a; else return c; else if(b > c) return b; else return c; */
int Max3( int A, int B, int C )
{
return A > B ? A > C ? A : C : B > C ? B : C;
}
int DivideAndConquer( int List[], int left, int right )
{
int MaxLeftSum, MaxRightSum; /* 存放左右子问题的解 */
int MaxLeftBorderSum, MaxRightBorderSum; /*存放跨分界线的结果*/
int LeftBorderSum, RightBorderSum;
int center, i;
/* 递归的终止条件,子列只有1个数字 */
if( left == right ) //求左右最大和的出口。
{
if( List[left] > 0 )
return List[left];
else
return 0;
}
/* 下面是"分"的过程 */
center = ( left + right ) / 2; /* 找到中分点 */
/* 递归求得两边子列的最大和 */
MaxLeftSum = DivideAndConquer( List, left, center );
MaxRightSum = DivideAndConquer( List, center+1, right );
/* 下面求跨分界线的最大子列和 */
/* 从中线向左扫描 */
MaxLeftBorderSum = 0; LeftBorderSum = 0;
for( i=center; i>=left; i-- )
{
LeftBorderSum += List[i];
if( LeftBorderSum > MaxLeftBorderSum )
MaxLeftBorderSum = LeftBorderSum;
}
/* 左边扫描结束 */
/* 从中线向右扫描 */
MaxRightBorderSum = 0; RightBorderSum = 0;
for( i=center+1; i<=right; i++ )
{
RightBorderSum += List[i];
if( RightBorderSum > MaxRightBorderSum )
MaxRightBorderSum = RightBorderSum;
}
/* 右边扫描结束 */
// 下面返回"治"的结果 ,返回三者最大值
return Max3( MaxLeftSum, MaxRightSum, MaxLeftBorderSum + MaxRightBorderSum );
}
运行结果:
算法四:
sum的变化为一段序列的和 以及下一段序列的和。。。每次结束一段序列都要清零,重新开始
结束条件:当前序列和为负数。
原因:如果当前序列为负数,则他往后加的结果并不能使序列和变大,则将其重新归零
<mark>复杂度:O(N)。</mark> 优秀
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <ctime>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
#define maxn 100005
#define mod 7654321
int main()
{
int a[maxn];
int n,sum=0,maxx=0;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
//sum的变化为一段序列的和 以及下一段序列的和。。。每次结束一段序列都要清零,重新开始
//结束条件:当前序列和为负数。
for(int i=0;i<n;i++)//左端点
{
sum+=a[i];
if(maxx<sum)//maxx永远存储某一序列最大值
maxx=sum;
if(sum<0)//如果当前序列为负数,则他往后加的结果并不能使序列和变大,则将其重新归零
sum=0;
}
cout<<maxx<<endl;
return 0;
}
运行结果:速度更快!!!