string and

substring and
Time Limit: 5000  ms | Memory Limit: 65535  KB
Difficulty: 3
describe
Given an integer sequence {a 1 ,a 2 ...,a n }, find a continuous non-empty subsequence {a x ,a x+1 ,...,a y } such that the sum of the subsequence is the largest , where 1<=x<=y<=n.
enter
The first line is an integer N (N<=10) represents the number of
groups of test data) The first line of each group of test data is an integer n represents a total of n integers in the sequence, and the next line contains n integers I ( -100=<I<=100), which means all elements in the sequence. (0<n<=1000000)
output
For each set of test data output the sum of the largest consecutive substring.
sample input
1
5
1 2 -1 3 -2
Sample output
5
hint
There is a lot of input data, it is recommended to use scanf for input


The maximum substring sum is done in this dynamic update method, which saves time. Let me talk about my personal understanding of this method:

No matter what the given sequence is, its maximum substring sum must exist, so the starting point is based on this. One variable tp counts the value that can be achieved by increasing the value of one element each time, and the other sum records the value. The maximum value, every time an element is added, tp changes, compare whether tp exceeds the current maximum value sum If it exceeds, then update sum to the value of tp, because the maximum value is now the value of tp, and then discuss the value of tp Update processing, it is easy to think that if the sum of the first n items (the value of tp starting from a certain starting point) is still positive, after adding the latter item, suddenly the tp becomes negative, then prove this The item is a very large negative number, so the sequence of the largest substring sum must not contain this element, and then continue to find the substring sum, and start directly from the next item. Skip the previous item and update it like this , the final result must be the sum of the largest substring, because we only update when the current substring and tp are greater than sum, otherwise it will not be updated, and the tp variable can sum all substrings that may reach the maximum sum. I have asked the sum all over again, so the maximum sum must be recorded. However, this substring is not recorded, only the maximum value is recorded. If you want to record this sequence, it is estimated that you will have to find a solution... .. I didn't understand it thoroughly, so I won't go into details for the time being...

Pay attention to the pit of this question, it cannot be initialized to 0 during initialization, it must be initialized to a minimum value......


 
#include<stdio.h>
#define maxn 1000005
int x[maxn],n;
void solve()
{
	int tp,sum;
	tp=0;sum=-maxn;
	for(int i=0;i<n;++i)
	{
		tp+=x[i];//accumulate
		if(tp>sum)//It is larger than the recorded value
		{
			sum=tp;//Update
		}
		else if(tp<0)//less than zero, start over
		{
			tp=0;
		}
	}
	printf("%d\n",sum);
}
intmain()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=0;i<n;++i)
		{
			scanf("%d",x+i);
		}
		solve();
	}
	return 0;
}                        

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324884709&siteId=291194637
Recommended