Educational Codeforces Round 21 D.Array Division(二分)

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

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.

题目链接:http://codeforces.com/contest/808/problem/D

题意:在数组中移动一个数 使得分组可以分割成两个数组 使得两个数组之和相等

分析:

首先分两种情况
1. 往后面移动一个数到前面  (维护前缀和 看看后面有没有符合条件的数)
2. 移掉一个数            (移掉第i个数 看看连续的数能不能符合条件) 

用二分做,观摩观摩

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int N=100010;
 5 ll s[N],a[N],sum;
 6 int n;
 7 bool find(int l,int r,ll x)
 8 {
 9     while(l<=r)
10     {
11         int mid=(l+r)/2;
12         if(s[mid]==x)
13             return 1;
14         if(s[mid]<x)
15             l=mid+1;
16         else r=mid-1;
17     }
18    return 0;
19 }
20 int main()
21 {
22     scanf("%d",&n);
23     for(int i=1;i<=n;i++)
24     {
25         scanf("%lld",&a[i]);
26         sum+=a[i];
27         s[i]=s[i-1]+a[i];//求前缀和
28     }
29     if(sum&1)
30     {
31         cout<<"NO"<<endl;
32         return 0;
33     }
34     sum/=2;
35     for(int i=1;i<=n;i++)
36     {
37         if(find(i+1,n,sum+a[i])||find(1,i-1,sum-a[i]))
38         {
39             cout<<"YES"<<endl;
40             return 0;
41         }
42     }
43     cout<<"NO"<<endl;
44     return 0;
45 }

 

目录
相关文章
|
2天前
|
Python
使用array()函数创建数组
使用array()函数创建数组。
18 3
|
2天前
|
JavaScript 前端开发
总结TypeScript 的一些知识点:TypeScript Array(数组)(下)
一个数组的元素可以是另外一个数组,这样就构成了多维数组(Multi-dimensional Array)。
|
2天前
|
存储 JavaScript 前端开发
总结TypeScript 的一些知识点:TypeScript Array(数组)(上)
数组对象是使用单独的变量名来存储一系列的值。
|
2天前
|
存储 安全 Swift
在Swift中,数组(Array)
在Swift中,数组(Array)
32 3
|
2天前
|
Ruby
|
7月前
|
存储 Java 索引
【面试题精讲】ArrayList 和 Array(数组)的区别?
【面试题精讲】ArrayList 和 Array(数组)的区别?
|
2天前
|
存储 索引 Python
多数pythoneer只知有列表list却不知道python也有array数组
多数pythoneer只知有列表list却不知道python也有array数组
27 0
|
2天前
|
JavaScript 前端开发 索引
在JavaScript中,可以使用数组字面量或Array构造函数来创建一个数组对象
【4月更文挑战第16天】在JavaScript中,可以使用数组字面量或Array构造函数来创建一个数组对象
23 4
|
6月前
|
算法 Python
数组倍增(Array Doubling
数组倍增(Array Doubling)是一种常见的算法技术,用于解决数组相关的查找、插入、删除等问题。该技术的核心思想是将数组的大小乘以 2,新数组的长度是原数组长度的两倍,然后将原数组中的元素复制到新数组中。在某些情况下,这种技术可以提高算法的效率,尤其是对于动态数据结构的问题。
140 1