Series longest sequence of questions a child

   Solving the largest contiguous subsequence and problems, the most direct way is to find and each successive sequence, and then to find the maximum, but the efficiency of this algorithm is O (n ^ 2), the following code,


   
int LS()
{
int Max =- 999999999 ;
for ( int i = 0 ;i < n;i ++ )
{
int sum = 0 ;
for ( int j =i ;j < n;j ++ )
{
sum
+= a[j];
if (sum > Max) Max = sum;
}
}
return Max;
}

 This algorithm is applicable to the case of small amount of data.

   In this method, there are many unnecessary overlap calculation, and it is these unnecessary calculations affect the performance of programs. In order to improve the efficiency of the algorithm, as much as possible to exclude unnecessary calculation, you can use the following algorithm, the algorithm efficiency is O (n).

  Improvement of this algorithm is that the elimination of the drawbacks of the previous method appear repeated several times.


   

// 该算法满足连续子序列求和问题:sum>=0的情况
// 如果有最大和为负数的情况,可以以特殊情况单独处理即可
for (i = 0 ;i < n;i ++ )
{
cin
>> a[i];
sum
+= a[i];
if (sum > Max)
{
Max
= sum;
start
= k + 1 ;
end
= i + 1 ;
}
if (sum < 0 ) // 关键点,消除不必要的重复
{
k
= i + 1 ;
sum
= 0 ;
}
}

 Algorithm idea:

    For each of the a [i], the post will be carried out by adding a sum of a prior comparison and, 

  IF (sum> Max)
   {
    Max = sum;
    start = K +. 1;
    End = I +. 1;
   }, wherein a maximum recording start location of the contiguous subsequence start, end recording its end position, while if the sum appears <0 case directly SUM = 0; because for any one sub-sequences, to ensure that the next sum> = 0 premise, negative numbers is not possible, it can be directly cleared to 0; at a time of need the starting position for recording, so that the situation sum> Max's next time if there is, you can start to re-assignment. 

      There is a point to note is that 0 <sum <Max case, in fact, be appreciated here It is good, if 0 <sum <Max, proceed + = a [i] sum, sum can not in this case cleared because sum is an incremental process, may sum> Max's situation at some point.

When do problems encountered two situations:

(1) http://acm.hdu.edu.cn/showproblem.php?pid=1003   the above process is to solve this problem

(2)http://acm.hdu.edu.cn/showproblem.php?pid=1231 

 Because of the emergence of the above to ensure that the sum> = 0 the situation, so the above processing algorithms do a little ok, processing is as follows:

  1.   Exclude all negative circumstances
  2.   The maximum exclusion and is 0

code show as below,


   
for (i = 0 ;i < n;i ++ )
{
cin
>> a[i];
if (a[i] < 0 ) count ++ ; //排除都为负数的情况
if (a[i] == 0 && i < L) L = i; //排除最大和为0的情况
sum
+= a[i];
if (sum > Max)
{
Max
= sum;
start
= k + 1 ;
end
= i + 1 ;
}
if (sum < 0 )
{
k
= i + 1 ;
sum
= 0 ;
}
}
if (Max > 0 ) cout << Max << " " << a[start - 1 ] << " " << a[end - 1 ] << endl;
else if (count != n && Max == 0 ) cout << Max << " " << a[L] << " " << a[L] << endl;
else cout << Max << " " << a[ 0 ] << " " << a[n - 1 ] << endl;

  

Reproduced in: https: //my.oschina.net/garyun/blog/602806

Guess you like

Origin blog.csdn.net/weixin_34124939/article/details/91774020