不要让昨天 占据你的今天 夏午晴天

夏午晴天

Array Division 808D

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.

 

题意:给定一个序列, 可以将一个元素的的位置随意移动。 问能否分为两个序列,使得两个序列和相等。

思路:先维护一个前缀和数组 sum[N]。 对于每个点可以将它移动到前一个序列 , 或则后一个序列两种情况。
        a     b     c      d       e       f        g       h       i        j
        对于 e 点分析 , 假如 分割点是 c ,它移动到第一序列 ,那么我们可以得到下面的公式:
        sum[c] + a[e] = sum [n] / 2;

  
        假如移动到后一序列 , 分割点是 f , 那么我们可以得到下面的公式: 
        sum
[f] a[e] = sum[n] / 2;

        所以如果可以找到一点 f, 则能分为两个序列,使得两个序列和相等。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 
 5 int a[123456];
 6 ll num[123456];
 7 
 8 bool check(int l, int r, ll x){
 9     while(l <= r){
10         int mid = l + r >> 1;
11         if(num[mid] == x)
12             return true;
13         if(num[mid] > x)
14             r = mid - 1;
15         if(num[mid] < x)
16             l = mid + 1;
17     }
18     return false;
19 }
20 
21 int main(){
22     int n;
23     cin >> n;
24     num[0] = 0;
25     for(int i = 1; i <= n; i++){
26         cin >> a[i];
27         num[i] = num[i - 1] + a[i];
28     }
29     if(num[n] & 1){
30         cout << "NO" << endl;
31         return 0;
32     }
33     for(int i = 1; i <= n; i++){
34         if(check(i + 1, n, num[n] / 2 + a[i])){
35             cout << "YES" << endl;
36             return 0;
37         }
38     }
39     for(int i = n; i >= 0; i--){
40         if(check(1, i - 1, num[n] / 2 - a[i])){
41             cout << "YES" << endl;
42             return 0;
43         }
44     }
45     cout << "NO" << endl;
46     return 0;
47 }

 

        

posted on 2017-05-20 12:11  夏晴天  阅读(251)  评论(0编辑  收藏  举报

导航

Live2D