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. Swift 2.0学习笔记(Day 16)——字典集合

    Swift字典表示一种非常复杂的集合,允许按照某个键来访问元素.字典是由两部分集合构成的,一个是键(key)集合,一个是值(value)集合.键集合是不能有重复元素的,而值集合是可以重复的,键和值是成 ...

  2. jenkins 添加节点问题

    没有 Launch agent via Java Web Start 选项 Manage Jenkins > Configure Global Security > TCP port fo ...

  3. SSM框架简介及整合教程

    1.Spring Spring 框架是 Java 应用最广的框架,它的成功来源于理念,而不是技术本身,它的理念包括 IoC (控制反转) 和 A面向切面编程).Spring框架是个轻量级的Java E ...

  4. MySql数据库常用语句汇总

    第一天1.登陆数据库 mysql -uroot -proot; //-u用户名 -p密码2.启动数据库 net start mysql;3.创建表空间(数据库)create database qy97 ...

  5. Mesh.CombineMeshes

    [Mesh.CombineMeshes] public void CombineMeshes(CombineInstance[] combine, bool mergeSubMeshes = true ...

  6. XSSExcelUtil

    package com.numa.util; import org.apache.poi.hssf.usermodel.*;import org.apache.poi.hssf.util.HSSFCo ...

  7. 解题2(IpIsSameSubNet)

    题目描述 子网掩码是用来判断任意两台计算机的IP地址是否属于同一子网络的根据.子网掩码与IP地址结构相同,是32位二进制数,其中网络号部分全为“1”和主机号部分全为“0”.利用子网掩码可以判断两台主机 ...

  8. python 多线程操作数据库

    如果使用多线程操作数据库,容易引起多用户操作锁表 OperationalError: (2013, 'Lost connection to MySQL server during query') 使用 ...

  9. JMeter学习(十五)JMeter测试Java(转载)

    转载自 http://www.cnblogs.com/yangxia-test 目的:对Java程序进行测试 目录 一.核心步骤 二.实例 三.JMeter Java Sampler介绍 四.自带Ja ...

  10. Ubuntu 16.04 LTS network DIASBLED解决办法

    问题 昨天正浏览着网页,突然无法连接.但右上角的wifi信号显示仍然是连接状态.于是我尝试断开再重新连接一次,没想到刚断开就报了个内部错误,然后wifi图标直接消失了.重启后虽然有wifi图标,但无法 ...