算法介绍
算法一:暴力法
不同的是并没有使用三重循环来进行遍历,因为观察到每一次只需要在a[i]后加一个即可,所以时间复杂度为O(n^2)。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int k = 0;
scanf("%d", &k);
int *a = (int*)malloc(sizeof(int) * k);
for(int i = 0; i < k; i++){
scanf("%d", &a[I]);
}
int maxsum = 0;
for(int i = 0; i < k; i++){
int thissum = 0;
for(int j = i; j < k; j++){
thissum += a[j];
if(thissum > maxsum){
maxsum = thissum;
}
}
}
free(a);
if(maxsum > 0){
printf("%d", maxsum);
}
else{
printf("0");
}
return 0;
}
算法二:在线处理法
在线处理法就是在输入的时候进行条件的判断,有点像对每一个数进行实时处理,在输入完成后结果也就得出,所以时间复杂度最低,为O(N)。具体的判断条件是如果输入的数加上目前的序列和之后导致了当前序列和的减少,则不进行录入,当相加后当前序列和小于0时,则放弃该序列,因为只会对之后的序列和产生副作用。缺点是不是很直观地让人理解。
#include <stdio.h>
int main()
{
int k = 0, x = 0;
scanf("%d", &k);
int thissum = 0, maxsum = 0;
for(int i = 0; i < k; i++){
scanf("%d", &x);
thissum += x;
if(thissum > maxsum){
maxsum = thissum;
}
else if(thissum < 0){
thissum = 0;
}
}
if(maxsum > 0){
printf("%d", maxsum);
}
else{
printf("0");
}
return 0;
}
算法三:分治法
这个方法较为复杂,就我个人而言理解和实现起来都比在线处理法麻烦不少,具体实现思路就是将一个规模比较大的问题通过不断划分划分成一个个小规模的问题,然后再将每个小问题的解集中起来,通过考虑到跨越边界的情况最终得出答案。(真是又复杂又难敲)
贴一个图帮助理解一下:
#include <stdio.h>
int Max3(int A, int B, int C){
//返回3个整数中的最大值
return A > B ? A > C ? A : C : B > C ? B : C;
//return A > B ? (A > C ? A : (B > C ? B : C)) : B > C ? B : C;
}
int DivideAndConquer( int List[], int left, int right )
{ /* 分治法求List[left]到List[right]的最大子列和 */
int MaxLeftSum, MaxRightSum; /* 存放左右子问题的解 */
int MaxLeftBorderSum, MaxRightBorderSum; /*存放跨分界线的结果*/
int LeftBorderSum, RightBorderSum;
int center, i;
if( left == right ) { /* 递归的终止条件,子列只有1个数字 */
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 );
}
int MaxSubseqSum3( int List[], int N )
{ /* 保持与前2种算法相同的函数接口 */
return DivideAndConquer( List, 0, N-1 );
}
int main()
{
int k = 0;
scanf("%d", &k);
int a[k];
for(int i = 0; i < k; i++){
scanf("%d", &a[i]);
}
int res = MaxSubseqSum3(a, k);
printf("%d", res);
return 0;
}
不过这代码看着工整舒服,递归还是没有明白。
练习:
题目网站:https://pintia.cn/problem-sets/994805342720868352/problems/994805514284679168
题目描述:
1007 Maximum Subsequence Sum (25 point(s))
Given a sequence of K integers { N1 , N2, ..., NK}. A continuous subsequence is defined to be { NI, NI+1, ..., Nj} where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.
Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.
Input Specification:
Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.
Output Specification:
For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.
Sample Input:
10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4
易错点分析
本题唯一的会影响AC的就是对于输出格式的要求, 一开始以为是输出对应的起止数据的下标,后来发现是输出对应的数,所以说样例还是很有误导性的,如果没有完全看清楚题目要求很容易在这卡住,只会得个辛苦分3分,出题的果然都想好了情况。对于输出的要求分为以下几种:
1.存在最大子序列,则输出max_sum以及对应的起止数据(如果最后一位是0的话,也要将0算进最大子序列中);
2.不存在最大子序列,输出0和输入的首尾数据;
3.输入全为负数,输出0和输入的首位数据;
4.输入全为负数和0,负数全部算作0,输出三个0.
这里面最后一个条件比较难以发现,同时也要注意与第三中情况区分开。
思路分析
下午刚学的方法就可以用,所以这道题最起码有三种解法,也就是上面的三种算法。
首先是在线处理法,我实现输出的思路是对最后符合要求的那个i记为z,说明到这结束,如果this_sum > max_sum, 则,z更新,同时子序列长度count++,最终只要将z减去长度再加一即可得到子序列首位的数据。如果是使用暴力法的话,就直接将q = i, z = j,不断更新即可,不再详细说明。实现思路上肯定是前者较为复杂,但是算法效率毋庸置疑是前者。
在线处理法:
#include <stdio.h>
int main()
{
int k = 10, x = 0;
scanf("%d", &k);
//int a[] = {-10, 1, 2, 3, 4, 1, -23, 2, 7, -21};
int a[k];
int this_sum = 0, max_sum = 0, q = 0, z = 0, count = 0, ret = 0, fu = 0, zero = 0;
for(int i = 0; i < k; i++){
scanf("%d", &a[i]);
if(a[i] == 0){
zero++;
}
if(a[i] < 0){
fu++;
}
}
if(fu+zero == k && zero != 0){
printf("0 0 0");
}
else if(fu == k){
printf("0 %d %d", a[0], a[k-1]);
}
else{
for(int i = 0; i < k; i++){
this_sum += a[i];
count++;
if(this_sum > max_sum){
max_sum = this_sum;
z = i;
ret = 1;
}
if(ret){
q = z - count+1;
}
if(this_sum < 0){
this_sum = 0;
count = 0;
}
ret = 0;
}
if(max_sum <= 0){
printf("0 %d %d", a[0], a[k-1]);
}
else{
printf("%d %d %d", max_sum, a[q], a[z]);
}
}
return 0;
}
暴力法(直接搬运https://blog.csdn.net/wwk0125/article/details/50444529)
#include<iostream>
#include<cstdio>
using namespace std;
#define maxN 10001
#define Inf 0x3f3f3f
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int a[maxN];
int k,sum,max=-Inf;
int L, R;
cin >> k;
for (int i = 0; i < k; i++)
cin >> a[i];
for (int i = 0; i < k; i++)
{
sum = 0;
for (int j = i; j < k; j++)
{
sum += a[j];
if (sum>max)
{
max = sum;
L = i;
R = j;
}
}
}
if (max<0) //全为负数时,按=0处理,输出头尾
cout << "0" << " " << a[0] << " " << a[k-1] << endl;
else
cout << max << " " <<a[L] << " " << a[R] << endl;
return 0;
}
/*
Sample Input:
10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4
*/
原本以为暴力法会有一个点超时,结果没有,早知道就直接暴力了。