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, 则能分为两个序列,使得两个序列和相等。

 #include <bits/stdc++.h>
using namespace std;
#define ll long long int a[];
ll num[]; bool check(int l, int r, ll x){
while(l <= r){
int mid = l + r >> ;
if(num[mid] == x)
return true;
if(num[mid] > x)
r = mid - ;
if(num[mid] < x)
l = mid + ;
}
return false;
} int main(){
int n;
cin >> n;
num[] = ;
for(int i = ; i <= n; i++){
cin >> a[i];
num[i] = num[i - ] + a[i];
}
if(num[n] & ){
cout << "NO" << endl;
return ;
}
for(int i = ; i <= n; i++){
if(check(i + , n, num[n] / + a[i])){
cout << "YES" << endl;
return ;
}
}
for(int i = n; i >= ; i--){
if(check(, i - , num[n] / - a[i])){
cout << "YES" << endl;
return ;
}
}
cout << "NO" << endl;
return ;
}

Array Division 808D的更多相关文章

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

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

  2. Array Division CodeForces - 808D (构造+实现)

    Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into t ...

  3. Codeforces 808D. Array Division

    题目大意 给定你一个长为\(n\)的序列,问能否在最多一次取出某一元素然后插入到某一点后可以将整个序列分成两段使得其两段的元素之和相同. \(n \leq 10^5\) 题解 发现插入操作实际上是让某 ...

  4. D. Array Division

    http://codeforces.com/contest/808/problem/D 一开始是没什么想法的,然后回顾下自己想题的思路,慢慢就想出来了.首先要找到是否有这样的一个位置使得: 前缀和 = ...

  5. Educational Codeforces Round 21 D - Array Division (前缀和+二分)

    传送门 题意 将n个数划分为两块,最多改变一个数的位置, 问能否使两块和相等 分析 因为我们最多只能移动一个数x,那么要么将该数往前移动,要么往后移动,一开始处理不需要移动的情况 那么遍历sum[i] ...

  6. 【multimap的应用】D. Array Division

    http://codeforces.com/contest/808/problem/D #include<iostream> #include<cstdio> #include ...

  7. codeforces 808 D. Array Division(二分)

    题目链接:http://codeforces.com/contest/808/problem/D 题意:有一串长度为n的数组,要求选择一个数字交换它的位置使得这串数能够分成两串连续的和一样的数组. 这 ...

  8. Codeforces D. Array Division

    题目链接:http://codeforces.com/contest/808/problem/D 题意: 这一题给你一个数组,你可以调换某一个数的位置,使得这个数组可以分成2半,前半段的和等于后半段( ...

  9. CF808D STL

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

随机推荐

  1. 安装opencv3.x卡在ICV: Downloading ippicv_linux_20151201.tgz...

    参考:http://blog.csdn.net/bobsweetie/article/details/52502741 可以自己下载: ICV: Downloading ippicv_linux_20 ...

  2. CodeForces-1132C-Painting the Fence-(前缀和)

    You have a long fence which consists of nn sections. Unfortunately, it is not painted, so you decide ...

  3. Mybatis之mapper.xml配置文件中的#{}和${}

    #{}表示一个占位符号,通过#{}可以实现preparedStatement向占位符中设置值,自动进行java类型和jdbc类型转换.#{}可以有效防止sql注入. #{}可以接收简单类型值或pojo ...

  4. 获取镜像tag

    # curl -k https://k8s.gcr.io/v2/fluentd-elasticsearch/tags/list|jq .tags % Total % Received % Xferd ...

  5. Java中的字节流,字符流,字节缓冲区,字符缓冲区复制文件

     一:创建方式 1.建立输入(读)对象,并绑定数据源 2.建立输出(写)对象,并绑定目的地 3.将读到的内容遍历出来,然后在通过字符或者字节写入 4.资源访问过后关闭,先创建的后关闭,后创建的先关闭 ...

  6. Java 面向切面 AOP

    参考: :http://www.blogjava.net/supercrsky/articles/174368.html AOP: Aspect Oriented Programming 即面向切面编 ...

  7. python--第五天总结

    装饰器-- @ 重命名原函数,返回函数对象 是一个函数,至少两层执行函数,被装饰的函数作为参数----------------------------------------------------1 ...

  8. java swing示例

    该范例主要是JFrame(框架)和Jpanel(画板),在Jpanel容器上添加控件,然后再把Jpanel放进JFrame的容器里面. FrameDemo.java import java.awt.D ...

  9. codis

    总体架构 192.168.199.223(zookeeper.codis-proxy.codis-dashborad:18080.codis-fe:18090.codis-server) 192.16 ...

  10. 反转链表(python)

    题目描述 输入一个链表,反转链表后,输出新链表的表头. # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self ...