CF 1006C Three Parts of the Array【双指针/前缀和/后缀和/二分】
You are given an array d1,d2,…,dn consisting of n 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 sum1, the sum of elements of the second part be sum2 and the sum of elements of the third part be sum3. Among all possible ways to split the array you have to choose a way such that sum1=sum3 and sum1 is maximum possible.
More formally, if the first part of the array contains a elements, the second part of the array contains b elements and the third part contains c elements, then:
sum1=∑1≤i≤adi,
sum2=∑a+1≤i≤a+bdi,
sum3=∑a+b+1≤i≤a+b+cdi.
The sum of an empty array is 0.
Your task is to find a way to split the array such that sum1=sum3 and sum1 is maximum possible.
Input
The first line of the input contains one integer n (1≤n≤2⋅105) — the number of elements in the array d.
The second line of the input contains n integers d1,d2,…,dn (1≤di≤109) — the elements of the array d.
Output
Print a single integer — the maximum possible value of sum1, considering that the condition sum1=sum3 must be met.
Obviously, at least one valid way to split the array exists (use a=c=0 and b=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
Note
In the first example there is only one possible splitting which maximizes sum1: [1,3,1],[ ],[1,4].
In the second example the only way to have sum1=4 is: [1,3],[2,1],[4].
In the third example there is only one way to split the array: [ ],[4,1,2],[ ].
【题意】:将一个长度为n的数组划分为a,b,c三段(每一段都可以为空)使得a段和c段的和相等,问a段的和的最大值是多少?
【分析】:双指针求前缀和与后缀和进行大小比较,一遇到相等时打擂台求最大。注意要求和所以用LL,不然会WA10!
【代码】:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
#define ll long long
ll n,m;
ll a[N];
/*
将一个长度为n的数组划分为a,b,c三段(每一段都可以为空)
使得a段和c段的和相等,问a段的和的最大值是多少?
*/
int main()
{
while(~scanf("%lld",&n))
{
ll Max = 0, ls, rs;
for(ll i=0; i<n; i++)
{
scanf("%lld",&a[i]);
}
ll L = 0, R = n-1;
ls = a[L];
rs = a[R];
while(L < R)
{
if(ls == rs)
{
Max = max(Max,ls);
L++;
R--;
ls += a[L];
rs += a[R];
}
else if(ls > rs)
{
R--;
rs += a[R];
}
else
{
L++;
ls += a[L];
}
}
cout<<Max<<endl;
}
}
【二分】:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 2*1e5+10;
ll n,a[maxn];
ll pre[maxn],suf[maxn];
/*
求前缀和,后缀和。然后从大到小枚举后缀,在前缀中查找相等,如果能找到且a,c两段不重叠,那么就是答案。注意开long long
*/
int main()
{
while(~scanf("%lld",&n))
{
ll Max = 0;
memset(pre,0,sizeof(pre));
memset(suf,0,sizeof(suf));
for(int i=0; i<n;i++)
{
scanf("%lld",&a[i]);
}
pre[0] = a[0];
for(int i=1;i<n;i++)
pre[i] = pre[i-1] + a[i];
suf[n-1] = a[n-1];
for(int i=n-2; i>=0; i--)
suf[i] = suf[i+1] + a[i];
for(int i=0; i<n; i++)
{
int pos = lower_bound(pre,pre+n,suf[i])-pre;
if(pos < i && pre[pos] == suf[i])
{
Max = max(Max,suf[i]);
}
}
printf("%lld\n",Max);
}
}
/*
n-1:4
0 1 2 3 4
1 2 3 4 5
5 9 12
*/