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
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 }
- 一步一步学lucene——(第三步:索引篇)
- 在Python应用中使用MongoDB
- Python检查xpath和csspath表达式是否合法
- 一步一步学lucene——(第四步:搜索篇)
- Python爬虫代理IP池
- SSDB图形界面管理工具:phpssdbadmin安装部署
- [Go 语言社区] 初始化内存数据--游戏列表数据
- SSDB安装配置记录
- Python标准库笔记(3) — datetime模块
- Django 1.10中文文档-第一个应用Part4-表单和通用视图
- Python标准库笔记(2) — re模块
- Python爬虫—破解JS加密的Cookie
- Go语言中json转成map结构
- rpc-dubbo简单入门
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 为PXC集群引入Mycat并构建完整的高可用集群架构
- Python3 字典
- 安装Percona Server数据库(in CentOS 8)
- Python 基础篇-正斜杠("/")和反斜杠("")的用法
- 在CentOS8下搭建PXC集群
- Python 基础篇-相对路径、绝对路径的写法
- Python3 元组
- 关于MySQL的基准测试
- Python 技术篇-操作excel,对excel进行读取和写入
- Mycat 整合 MySQL 8.x 踩坑实践
- Python 技术篇-xlwt库不新建,直接读取已存在的excel并写入
- Python3 列表
- Mycat 核心配置详解
- Python字符串
- 初识 HBase