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个数 看看连续的数能不能符合条件)

用二分做,观摩观摩

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=;
ll s[N],a[N],sum;
int n;
bool find(int l,int r,ll x)
{
while(l<=r)
{
int mid=(l+r)/;
if(s[mid]==x)
return ;
if(s[mid]<x)
l=mid+;
else r=mid-;
}
return ;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
sum+=a[i];
s[i]=s[i-]+a[i];//求前缀和
}
if(sum&)
{
cout<<"NO"<<endl;
return ;
}
sum/=;
for(int i=;i<=n;i++)
{
if(find(i+,n,sum+a[i])||find(,i-,sum-a[i]))
{
cout<<"YES"<<endl;
return ;
}
}
cout<<"NO"<<endl;
return ;
}

Educational Codeforces Round 21 D.Array Division(二分)的更多相关文章

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

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

  2. Educational Codeforces Round 21

    Educational Codeforces Round 21  A. Lucky Year 个位数直接输出\(1\) 否则,假设\(n\)十进制最高位的值为\(s\),答案就是\(s-(n\mod ...

  3. Educational Codeforces Round 21 Problem F (Codeforces 808F) - 最小割 - 二分答案

    Digital collectible card games have become very popular recently. So Vova decided to try one of thes ...

  4. Educational Codeforces Round 21 Problem D(Codeforces 808D)

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

  5. Educational Codeforces Round 11 C. Hard Process 二分

    C. Hard Process 题目连接: http://www.codeforces.com/contest/660/problem/C Description You are given an a ...

  6. Educational Codeforces Round 26 F. Prefix Sums 二分,组合数

    题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...

  7. Educational Codeforces Round 11A. Co-prime Array 数学

    地址:http://codeforces.com/contest/660/problem/A 题目: A. Co-prime Array time limit per test 1 second me ...

  8. Educational Codeforces Round 21(A.暴力,B.前缀和,C.贪心)

    A. Lucky Year time limit per test:1 second memory limit per test:256 megabytes input:standard input ...

  9. Educational Codeforces Round 21 Problem E(Codeforces 808E) - 动态规划 - 贪心

    After several latest reforms many tourists are planning to visit Berland, and Berland people underst ...

随机推荐

  1. ABP .Net Core API和Angular前端APP集成部署

    前言:在ABP官网(https://aspnetboilerplate.com)生成的.Net Core + Angular项目前后端是两个独立的项目,我们可以分开部署,也可以将前端和Web API一 ...

  2. signalr中Group 分组群发消息的简单使用

    前一段时间写了几篇关于signalr的文章 1.MVC中使用signalR入门教程 2.mvc中signalr实现一对一的聊天 3.Xamarin android中使用signalr实现即时通讯 在平 ...

  3. bzoj 3597: [Scoi2014]方伯伯运椰子

    Description Input 第一行包含二个整数N,M 接下来M行代表M条边,表示这个交通网络 每行六个整数,表示Ui,Vi,Ai,Bi,Ci,Di 接下来一行包含一条边,表示连接起点的边 Ou ...

  4. OpenGL ES学习001---绘制三角形

    PS:OpenGL ES是什么? OpenGL ES (OpenGL for Embedded Systems) 是 OpenGL三维图形 API 的子集,针对手机.PDA和游戏主机等嵌入式设备而设计 ...

  5. package-cleanup

    package-cleanup 是一个python开发的命令程序,用来清除本机已安装的.重复的 或孤立的软件包. desktop版的CentOS镜像包含这个工具,而Minimal版的CentOS镜像不 ...

  6. locust 参数,数据详解

    参数    说明-h, –help    查看帮助-H HOST, –host=HOST    指定被测试的主机,采用以格式:http://10.21.32.33–web-host=WEB_HOST  ...

  7. Python中range()和len()

  8. java RTTI笔记 之Class学习笔记(摘自java编程思想)

    1.java 使用Class对象来执行其RTTI.java 中每个类在编译后都会对应产生一个Class对象(更恰当地说是被保存在一个同名的.class文件中),甚至void和基本类型也都对应一个cla ...

  9. Python文件读写 - 文件r+ a+ open读写实际表现[示例]

    先说结论: 文件r+ open: 1. write()不能实现插入写,它总是覆盖写或附加写: 2. 如果文件一打开即write(),则从开头覆盖写; 3. 如果文件一打开,用f.seek()指定文件指 ...

  10. icon图标和文字整体居中在button按钮

    icon图标和文字整体居中在button按钮 icon图标和文字整体居中 一般我们常做的button按钮是文字居中 现在这个需要icon图标和文字一起居中在背景色 <a href="# ...