CodeForces - 1006C Three Parts of the Array (前缀和 + 尺取思想)

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],[ ].

题意:给你一个数组,要求你将数组分成三段,第一段为 第 1 到  a 个数,第二段为第 a + 1 到 b 个数,第三段为 b + 1 到最后一个数。每一段都可以为空。设第一段和为 sum1, 第二段为sum2.第三段为sum3, 现要求 sum1 = sum3,且 sum1 最大, 输出sum1 的最大值。

思路:很像尺取法,设定一个头指针,一个尾指针,头指针代表第一段的最后一个数,尾指针 + 1 代表第三段的第一个数,每一次判断sum1 和 sum3 的大小,决定指针移动就可以了。

AC代码:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define INT(t) int t; scanf("%d",&t)
#define LLI(t) LL t; scanf("%I64d",&t)

using namespace std;

const int maxn = 2e5 + 10;
int d[maxn];
LL sum[maxn];

int main()
{
    int n;
    while(~scanf("%d",&n)){
        memset(sum,0LL,sizeof(sum));
        rep(i,1,n + 1){
            scanf("%d",&d[i]);
            sum[i] = sum[i - 1] + (LL)d[i];
        }
        int l = 0,r = n;
        int ans = 0;
        while(l <= r){
            if(sum[l] == sum[n] - sum[r]){
                ans = l;
                ++ l;
                -- r;
            }
            else if(sum[l] < sum[n] - sum[r])
                ++ l;
            else -- r;
        }
        printf("%I64d\n",sum[ans]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/no_O_ac/article/details/82890044