Codeforces 808D Array Division【思维】

D. Array Division
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position).

Inserting an element in the same position he was erased from is also considered moving.

Can Vasya divide the array after choosing the right element to move and its new position?

Input

The first line contains single integer n (1 ≤ n ≤ 100000) — the size of the array.

The second line contains n integers a1, a2... an (1 ≤ ai ≤ 109) — the elements of the array.

Output

Print YES if Vasya can divide the array after moving one element. Otherwise print NO.

Examples
Input
3
1 3 2
Output
YES
Input
5
1 2 3 4 5
Output
NO
Input
5
2 2 3 4 5
Output
YES
Note

In the first example Vasya can move the second element to the end of the array.

In the second example no move can make the division possible.

In the third example Vasya can move the fourth element by one position to the left.


题目大意:

给你一个长度为N的序列,让你将其分成连续的两部分(不能为空),使得两部分的和都相等。

我们在分序列之前,可以选择不移动或者移动一个数到任意一个位子。


思路:


很套路,但是很简单。

我们首先维护出一个前缀和。

然后O(n)枚举分界线。

那么前一部分的和为Sumpre,后一部分的和为Sumback.

一共有三种情况:
①Sumpre==Sumback.那么结果就是YES.

②Sumpre>Sumback.那么我们考虑将前一部分的序列中,调出一个值为(Sumpre-Sumback)/2的值即可。这里map维护一下

③Sumpre<Sumback.那么我们考虑将后一部分的序列中,调出一个值为(Sumback-Sumpre)/2的值即可。这里map维护一下


注意序列不能为空。

注意奇偶。

以及某个值A【i】==sum【n】/2的情况也是YES.


Ac代码:

#include<stdio.h>
#include<string.h>
#include<map>
using namespace std;
#define ll __int64
ll a[1000050];
ll sum[1000050];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int flag=0;
        map<ll,int >pre;
        map<ll,int >back;
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=n;i++)scanf("%I64d",&a[i]);
        for(int i=1;i<=n;i++)sum[i]=sum[i-1]+a[i];
        for(int i=1;i<=n;i++)back[a[i]]++;
        for(int i=1;i<=n;i++)if(sum[n]%2==0&&a[i]==sum[n]/2)flag=1;
        for(int i=1;i<n;i++)
        {
            back[a[i]]--;
            pre[a[i]]++;
            ll Sumpre=sum[i];
            ll Sumback=sum[n]-sum[i];
            if(Sumpre==Sumback)flag=1;
            else
            {
                if(Sumpre>Sumback)
                {
                    if(i==1)continue;
                    if((Sumpre-Sumback)%2==1)continue;
                    ll x=(Sumpre-Sumback)/2;
                    if(pre[x]>0)flag=1;
                }
                else
                {
                    if(i==n-1)continue;
                    if((Sumback-Sumpre)%2==1)continue;
                    ll x=(Sumback-Sumpre)/2;
                    if(back[x]>0)flag=1;
                }
            }
        }
        if(flag==1)printf("YES\n");
        else printf("NO\n");
    }
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值