【Luogu】P1115 Maximum sub-segment sum (greedy)

【Luogu】P1115 Maximum subsection sum

Problem description
Given a sequence, select a continuous and non-empty segment to maximize the sum of this segment.

Input format
The first line is a positive integer N (0<N<200000), indicating the length of the sequence.
The second line contains N integers Ai whose absolute value is not greater than 10000, describing this sequence.

output format
An integer that is the largest sum of subsections. Subsections have a minimum length of 1.

Input example
7
2 -4 3 -1 2 -4 3

Output sample
4

The example shows that
in 2,−4,3,−1,2,−4,3, the largest sub-segment sum is 4, and the sub-segment is 3,−1,2.


Algorithm Analysis
This question is a greedy algorithm question. When looking for the largest sub-segment, we can of course find it through enumeration, that is, maintain a prefix and an array of length N, and then compare the sum of the sub-segment sequences calculated by the prefix and in turn, and output the maximum value, but that Obviously it is extremely time-consuming (and there may be variable overflow). At this point, we can use a temporary prefix and the variable sum to record the sum of the current sequence. For any sub-section, if the sum of the current sub-section is to keep increasing during the backward extension process, then for any subsequent input value, it should be a positive number, which must ensure that the current sub-section And the sum is developed in an increasing direction.
However, this way of thinking is wrong! For example, in the example given in the question, although there is a negative number -1 after 3, there is a positive number 2 after that that can make up for the loss caused by -1 and increase the sum, so the above idea is not rigorous. That is: the maximum sum of subsections does not mean that the contents of the subsections are all positive numbers.
From here we can find a key point, that is, we should not pay attention to the positive or negative of a certain value input in the current sequence, but to look at the positive or negative of the previous temporary prefix and the variable sum. Obviously, when sum > 0, it means that adding the previous sub-section will increase the sum of the sequence, so sum = sum + num can be updated; otherwise, it means that adding the entire previous sub-section will only reduce the sum of the current sub-section , so this subsection needs to be discarded, so sum = 0 needs to be set again. Next, you only need to set another variable max for storing the maximum value to save the maximum sub-segment sum that appears in the whole process. The complete code for solving this problem is given below.

#include<iostream>
using namespace std;

int main()
{
    
    
	int n,sum,max,now;	// sum用于记录前缀和,max用于记录最大值,now用于记录当前输入的值
	cin>>n>>sum; 		// 将输入序列的第一个作为sum
	max = sum;			// 将sum的值作为max的值
	while(--n)
	{
    
    
		cin>>now;
		sum = sum>0?sum:0; 	// 判断sum的正负以决定是否保留该序列
		sum += now;			// 无论怎样,都加上当前输入的值以对比max
		max=max>sum?max:sum;
	}
	cout<<max<<endl;
	return 0;
}

Advanced topic: [Blue Bridge Cup] The largest sub-array of previous test questions


END


Guess you like

Origin blog.csdn.net/the_ZED/article/details/123648922
Recommended