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
  1. 3
    1 3 2
Output
  1. YES
Input
  1. 5
    1 2 3 4 5
Output
  1. NO
Input
  1. 5
    2 2 3 4 5
Output
  1. 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=;
  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)/;
  12. if(s[mid]==x)
  13. return ;
  14. if(s[mid]<x)
  15. l=mid+;
  16. else r=mid-;
  17. }
  18. return ;
  19. }
  20. int main()
  21. {
  22. scanf("%d",&n);
  23. for(int i=;i<=n;i++)
  24. {
  25. scanf("%lld",&a[i]);
  26. sum+=a[i];
  27. s[i]=s[i-]+a[i];//求前缀和
  28. }
  29. if(sum&)
  30. {
  31. cout<<"NO"<<endl;
  32. return ;
  33. }
  34. sum/=;
  35. for(int i=;i<=n;i++)
  36. {
  37. if(find(i+,n,sum+a[i])||find(,i-,sum-a[i]))
  38. {
  39. cout<<"YES"<<endl;
  40. return ;
  41. }
  42. }
  43. cout<<"NO"<<endl;
  44. return ;
  45. }

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. iOS 面试题、知识点 之一

    最近面试,发现这些题个人遇到的几率大一些,与大家分享一下,分三文给大家: 当然Xcode新版本与之前一版本的区别,以及iOS新特性是必要了解的吧. Xcode8 和iOS 10 在之前文章有发过,感兴 ...

  2. akka-stream与actor系统集成以及如何处理随之而来的背压问题

    这几天上海快下了五天的雨☔️☔️☔️☔️,淅淅沥沥,郁郁沉沉.     一共存在四个api: Source.actorRef,返回actorRef,该actorRef接收到的消息,将被下游消费者所消费 ...

  3. Java 集成 速卖通开发.

    一.申请成为开发者 申请入口:http://isvhz.aliexpress.com/isv/index.htm 说明文档:http://activities.aliexpress.com/open/ ...

  4. MySQL 如何存储长度较大的varchar与blob

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/96 最近,在工作中遇到了MySQL中如何存储长度较长的字段类型问 ...

  5. xamarin android如何监听单击事件

    在xamarin android单击事件是最基础的事情,看过菜鸟上的android教程时,java写的都是监听事件,为一个按钮,单选按钮.多选按钮的单击事件有三种,前面两种用的非常普遍,也很简易,我这 ...

  6. css scroll bug

    滚动区域不能设置overflow var doc = $(document), win = $(window), h = $("#head"), b = $("#body ...

  7. 跟我一起,利用bitcms内容管理系统从0到1学习小程序开发:一、IIS下SSL环境搭建

    缘起 1.从事互联网十来年了,一直想把自己的从事开发过程遇到的问题给写出来,分享给大家.可是可是这只是个种想法,想想之后就放下了,写出来的类文章是少之又少.古人说无志之人常立志,有志之人立长志.今天, ...

  8. Bootstrap File Input的简单使用

    安装引入 使用前需要引入其css和js文件, 注意引入路径的问题 <link rel="stylesheet" href="/__PUB__/fileinput/c ...

  9. 扎实基础之从零开始-Nginx集群分布式.NET应用

    1       扎实基础之快速学习Nginx Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.其特点是占有内存少 ...

  10. Spring Cloud Consul入门

    1. Consul介绍 Consul是一套开源的分布式服务发现和配置管理系统,支持多数据中心分布式高可用.Consul是HashiCorp( Vagrant的创建者)开发的一个服务发现与配置项目,用G ...