标签:
给定K个整数组成的序列{ N1, N2, ..., NK },“连续子列”被定义为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j <= K。“最大子列和”则被定义为所有连续子列元素的和中最大者。例如给定序列{ -2, 11, -4, 13, -5, -2 },其连续子列{ 11, -4, 13 }有最大的和20。现要求你编写程序,计算给定整数序列的最大子列和。
输入格式:
输入第1行给出正整数 K (<= 100000);第2行给出K个整数,其间以空格分隔。
输出格式:
在一行中输出最大子列和。如果序列中所有整数皆为负数,则输出0。
输入样例:6
-2 11 -4 13 -5 -2
输出样例:
20
此题在姥姥的视频里有讲,一共讲了4种方法,其中最优的是在线算法,此处我不再赘述,直接贴上代码,详情请看姥姥的视频讲解。
1 #include<iostream> 2 using namespace std; 3 4 int main() 5 { 6 int N; 7 cin >> N; 8 9 int this_sum=0; 10 int max_sum=0; 11 int element=0; 12 13 for (int i=0;i<N;i++) 14 { 15 cin >> element; 16 this_sum += element; 17 if (this_sum > max_sum) 18 max_sum = this_sum; 19 if (this_sum < 0) 20 this_sum = 0; 21 } 22 23 if (max_sum<0) 24 cout << 0; 25 else 26 cout << max_sum; 27 28 return 0; 29 }
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 -21Sample Output:
10 1 4
这题与上一题类似,只不过多出了一个要求:输出最大子列的首尾元素。此题我的做法是,用两个变量l_temp和r_temp记录当前子列的首尾元素,如果该子列的和比当前记录的最大和要大,则用这两个变量更新最大子列的首尾元素l_max和r_max。如果当前子列的和小于0,则从下一个元素开始重新计算。但是有几个特殊点需要注意:
1.如果输入的元素全为负数,则最大和为0,同时输出首尾的负数。
2.如果输入元素全为负数和0,则最大和为0,但输出应该是0 0 0.
比如输入-3 0 -6,输出是0 0 0,而不是0 -3 -6
3.输出的首尾元素应该具有最小下标,比如:
输入3 7 -10 11,则应输出11 3 11,而不是11 11 11
1 #include<iostream> 2 using namespace std; 3 4 int main() 5 { 6 int N; 7 cin >> N; 8 9 int this_sum=0; 10 int max_sum=0; 11 int l_max=0; 12 int r_max=0; 13 int l_temp=0; 14 int r_temp=0; 15 bool begin_again=false;//判断this_sum是否重新开始 16 bool allnegative=true;//判断数组元素是否全为0 17 18 int *element = new int [N]; 19 for (int i=0;i<N;i++) 20 { 21 cin >> element[i]; 22 } 23 24 l_temp=element[0]; 25 r_temp=element[0]; 26 for (int i=0;i<N;i++) 27 { 28 this_sum += element[i]; 29 30 if (element[i]>=0) 31 allnegative=false; 32 33 if (begin_again) 34 { 35 l_temp=element[i]; 36 r_temp=element[i]; 37 begin_again=false; 38 } 39 else 40 { 41 r_temp=element[i]; 42 } 43 44 if (this_sum > max_sum) 45 { 46 max_sum = this_sum; 47 l_max = l_temp; 48 r_max=r_temp; 49 } 50 else 51 if (this_sum < 0)//只能用<0,不能<=0,因为要输出smallest indices,若3 7 -10 11,则应输出11 3 11,而不是11 11 11 52 { 53 this_sum = 0; 54 begin_again=true;//重新开始 55 } 56 } 57 58 if (max_sum==0) 59 if (allnegative) 60 cout << 0 << ‘ ‘ << element[0] << ‘ ‘ << element[N-1] << endl; 61 else 62 cout << 0 << ‘ ‘ << 0 << ‘ ‘ << 0 << endl; 63 else 64 cout << max_sum << ‘ ‘ << l_max << ‘ ‘ << r_max << endl; 65 66 return 0; 67 }
标签:
原文地址:http://www.cnblogs.com/xian-ye/p/5193854.html