Codeforces-808D Array Division 【multiset】

Description

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.

 


 

 

题解:

 

题意是给定一个数列,移动0或1个数字,使数列能从某个位置分开前后两半的和相等。我们可以假想有个隔板从左向右扫,每次两边的和的差值为x,若能找到某一边(看那边大)的一个数为x/2,则将它放到另一边就满足条件。扫一遍O(n),数据是无序的,如果暴力查找则总复杂度O(n^2)无法承受,我们可以维护两个平衡树做到O(nlogn),这里我们发现完全可以用STL的multiset代替。

 

代码:

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define INF 0x3f3f3f3f
 4 #define M(a, b) memset(a, b, sizeof(a))
 5 const int N = 100000 + 5;
 6 long long a[N];
 7 multiset<long long> s1, s2;
 8 multiset<long long>::iterator it;
 9 
10 int main() {
11     ios::sync_with_stdio(false);
12     int n;
13     while (cin >> n) {
14         s1.clear(); s2.clear();
15         long long sum = 0;
16         for (int i = 1; i <= n; ++i) cin >> a[i], sum += a[i], s2.insert(a[i]);
17         if (sum & 1) {cout << "NO\n"; continue;}
18         long long sum1 = 0, sum2 = sum;
19         if (s2.count(sum/2)) {cout << "YES\n"; continue;}
20         bool flag = 0;
21         long long temp;
22         for (int i = 1; i <= n; ++i) {
23             if (sum1 == sum2) {flag = 1; break;}
24             if (sum1 < sum2) {
25                 temp = sum2-sum1;
26                 if (temp%2==0 && s2.count(temp/2)) {flag = 1; break;}
27             }
28             else {
29                 temp = sum1-sum2;
30                 if (temp%2==0 && s1.count(temp/2)) {flag = 1; break;}
31             }
32             sum1 += a[i]; s1.insert(a[i]);
33             sum2 -= a[i]; it = s2.find(a[i]); s2.erase(it);
34         }
35         if (flag) cout << "YES\n";
36         else cout << "NO\n";
37 
38     }
39 
40     return 0;
41 }

转载于:https://www.cnblogs.com/robin1998/p/6864278.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值