Codeforces 808D Array Division【思维】

来源:互联网 发布:淘宝购物如何追加评论 编辑:程序博客网 时间:2024/05/19 16:32

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 printNO.

Examples
Input
31 3 2
Output
YES
Input
51 2 3 4 5
Output
NO
Input
52 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 __int64ll 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");    }}







原创粉丝点击