本文主要是介绍CodeForces - 1006CThree Parts of the Array暴力模拟,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 ).
Examples
Input
5 1 3 1 1 4
Output
5
Input
5 1 3 2 1 4
Output
4
Input
3 4 1 2
Output
0
思路:通过在数组的两端定义两个指针,判断前后两个数分别向前移动,向后移动的和的大小,
在分别进行讨论,尤其注意相等时
#include<cstdio>
using namespace std;
long long a[200008];
int main()
{int n;long long ans=0;scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%lld",&a[i]);long long i=1,j=n,l=a[1],r=a[n];while(i<j){if(l<r){i++;l+=a[i];}else if(l>r){j--;r+=a[j];}else{ans=l;i++;l+=a[i];}}printf("%lld\n",ans);return 0;
}
这篇关于CodeForces - 1006CThree Parts of the Array暴力模拟的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!