C. THREE PARTS OF THE ARRAY【双头指针+前缀和】

http://www.yyycode.cn/index.php/2020/05/18/c-three-parts-of-the-array%e3%80%90%e5%8f%8c%e5%a4%b4%e6%8c%87%e9%92%88%e5%89%8d%e7%bc%80%e5%92%8c%e3%80%91/


You are given an array d1,d2,…,dnd1,d2,…,dn consisting of nn integer numbers.

Your task is to split this array into three parts (some of which may be empty) in such a way that each element of the array belongs to exactly one of the three parts, and each of the parts forms a consecutive contiguous subsegment (possibly, empty) of the original array.

Let the sum of elements of the first part be sum1sum1, the sum of elements of the second part be sum2sum2 and the sum of elements of the third part be sum3sum3. Among all possible ways to split the array you have to choose a way such that sum1=sum3sum1=sum3 and sum1sum1 is maximum possible.

More formally, if the first part of the array contains aa elements, the second part of the array contains bb elements and the third part contains cc elements, then:sum1=∑1≤i≤adi,sum1=∑1≤i≤adi,sum2=∑a+1≤i≤a+bdi,sum2=∑a+1≤i≤a+bdi,sum3=∑a+b+1≤i≤a+b+cdi.sum3=∑a+b+1≤i≤a+b+cdi.

The sum of an empty array is 00.

Your task is to find a way to split the array such that sum1=sum3sum1=sum3 and sum1sum1 is maximum possible.Input

The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in the array dd.

The second line of the input contains nn integers d1,d2,…,dnd1,d2,…,dn (1≤di≤1091≤di≤109) — the elements of the array dd.Output

Print a single integer — the maximum possible value of sum1sum1, considering that the condition sum1=sum3sum1=sum3 must be met.

Obviously, at least one valid way to split the array exists (use a=c=0a=c=0 and b=nb=n).ExamplesinputCopy

扫描二维码关注公众号,回复: 11266598 查看本文章
5
1 3 1 1 4

outputCopy

5

inputCopy

5
1 3 2 1 4

outputCopy

4

inputCopy

3
4 1 2

outputCopy

0

Note

In the first example there is only one possible splitting which maximizes sum1sum1: [1,3,1],[ ],[1,4][1,3,1],[ ],[1,4].

In the second example the only way to have sum1=4sum1=4 is: [1,3],[2,1],[4][1,3],[2,1],[4].

In the third example there is only one way to split the array: [ ],[4,1,2],[ ][ ],[4,1,2],[ ].


题意:划分区间为三份,令左边的sum1==最右边sum3,中间的sum2不能没有,sum1==sum3可以为0,求最大的sum1

思路:对左到右前缀和一遍,对右到左前缀和一遍,然后两头双指针,往中间走找到最大的sum1。

代码上注意:i!=j不够,可能i++,j–导致跳出判断死循环,当要跳的时候就break结束双指针

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=2e5+10;
typedef long long LL;
LL a[maxn];
LL sum1[maxn];
LL sum2[maxn];
int main(void)
{
	LL n;cin>>n;
	for(LL i=1;i<=n;i++)
		cin>>a[i];
	//正的前缀和 
	for(LL i=1;i<=n;i++)
		sum1[i]=sum1[i-1]+a[i];

	//倒的前缀和 
	for(LL i=n;i>=1;i--)
		sum2[i]=sum2[i+1]+a[i];

	LL res=0;
	for(LL i=1,j=n; i!=j; )
	{
		if(sum1[i]==sum2[j])	
		{
			res=max(res,sum1[i]);
			i++;j--;
			if(i>j) break;
		}
		else if(sum1[i]<sum2[j])
		{
			i++;
		}
		else j--;
	}	
	cout<<res<<endl;
return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/106202742