【总结】最长连续子序列详细解法汇总


求最长连续子序列的模板题:HDU 1003 Max Sum

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


Recommend

We have carefully selected several similar problems for you: 1176 1087 1069 1058 1203


思路:题目大意,给定一个数组,a[1], a[2], …, a[n],求它的一个连续子序列,使得这个连续子序列的和最大。方法很多,可以暴力求解,也可以用动态规划求解。

第一种:暴力 O(N^3)

这种方法最直观,用三层循环遍历所有的连续子序列,不断的比较,取和最大的一段连续子序列,同时记录子序列的起始、结束索引。但是这种方法会TLE。

代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=1e6;
int a[maxn];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int t;	cin>>t;
	for(int k=1;k<=t;k++){
		int n;	cin>>n;
		for(int i=1;i<=n;i++)
			cin>>a[i];
		int start=0,end=0;
		int ans=-INF;
		for(int i=1;i<=n;i++){
			for(int j=i;j<=n;j++){
				int sum=0;
				for(int h=i;h<=j;h++)
					sum+=a[h];
				if(sum>ans){
					ans=sum;
					start=i;
					end=j;
				} 
			} 
		}
		cout<<"Case "<<k<<":"<<endl;
		cout<<ans<<" "<<start<<" "<<end<<endl; 
	}
} 


第二种:暴力+预处理 O(N^2)

这种方法比第一种复杂度低,两层循环。数组sum[i]先全部初始化为0,然后求出从a[0]开始到a[i]的和sum[i] (0<=i<=n)。然后双层循环,求出差值sum[j]-sum[i],即连续子序列之和。同时,不断的比较,取和最大的一段连续子序列,记录子序列的起始、结束索引。不幸的是,这种方法也会TLE。

代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=1e6;
int a[maxn],sum[maxn];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int t;	cin>>t;
	for(int k=1;k<=t;k++){
		int n;	cin>>n;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			sum[i]=sum[i-1]+a[i];
		}
		int start=0,end=0;
		int ans=-INF;
		for(int i=0;i<=n;i++){
			for(int j=i+1;j<=n;j++){
				int x=sum[j]-sum[i];
				if(x>ans){
					ans=x;
					start=i+1;
					end=j;
				} 
			}
		} 
		cout<<"Case "<<k<<":"<<endl;
		cout<<ans<<" "<<start<<" "<<end<<endl; 
	}
} 


第三种:暴力 O(N^2)

这种方法思路其实跟前两种差不多,用双层循环遍历,求连续子序列之和。时间复杂度还是O(N^2),所以很不幸,又TLE了。

代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=1e6;
int a[maxn];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int t;	cin>>t;
	for(int k=1;k<=t;k++){
		int n;	cin>>n;
		for(int i=1;i<=n;i++)
			cin>>a[i];
		int start=0,end=0;
		int ans=-INF;
		for(int i=1;i<=n;i++){
			int sum=0;
			for(int j=i;j<=n;j++){
				sum+=a[j];
				if(sum>ans){
					ans=sum;
					start=i;
					end=j;
				} 
			} 
		}
		cout<<"Case "<<k<<":"<<endl;
		cout<<ans<<" "<<start<<" "<<end<<endl; 
	}
} 


第四种:累积遍历求和 O(N)

给定数组,a[0], a[1], …, a[i], …,a[j], …, a[n-1],设S[i,j] = a[i]+a[i+1]+…+a[j],如果S[i, j] + a[j+1] < a[j+1],即S[i, j]<0, 则连续子序列{a[i], …, a[j], a[j+1]}这个子序列的和一定是最大的,所以子序列{a[i], …, a[j]}就不需要增长了,而应从a[j+1]开始重新增长子序列。

一层循环,时间复杂度O(N),终于AC了。要注意的就是输出格式,容易PE。

AC代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=1e6;
int a[maxn];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int t;	cin>>t;
	for(int k=1;k<=t;k++){
		int n;	cin>>n;
		for(int i=1;i<=n;i++)
			cin>>a[i];
		int ans=-INF,sum=0;
		int start=1,end=0;
		int starttemp=1,endtemp=0;
		for(int i=1;i<=n;i++){
			sum+=a[i];
			endtemp++;
			if(sum>ans){
				ans=sum;
				start=starttemp;
				end=endtemp;
			} 
			if(sum<0){
				sum=0;
				starttemp=i+1;
				endtemp=i;
			}
		}
		cout<<"Case "<<k<<":"<<endl;
		cout<<ans<<" "<<start<<" "<<end<<endl;
		if(k!=t)	cout<<endl; 	//有点坑 
	}
} 



后续待更新

你可能感兴趣的:(【总结】最长连续子序列详细解法汇总)