Array Division 808D
2 seconds
256 megabytes
standard input
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?
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.
Print YES if Vasya can divide the array after moving one element. Otherwise print NO.
3
1 3 2
YES
5
1 2 3 4 5
NO
5
2 2 3 4 5
YES
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的更多相关文章
- 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 ...
- Array Division CodeForces - 808D (构造+实现)
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into t ...
- Codeforces 808D. Array Division
题目大意 给定你一个长为\(n\)的序列,问能否在最多一次取出某一元素然后插入到某一点后可以将整个序列分成两段使得其两段的元素之和相同. \(n \leq 10^5\) 题解 发现插入操作实际上是让某 ...
- D. Array Division
http://codeforces.com/contest/808/problem/D 一开始是没什么想法的,然后回顾下自己想题的思路,慢慢就想出来了.首先要找到是否有这样的一个位置使得: 前缀和 = ...
- Educational Codeforces Round 21 D - Array Division (前缀和+二分)
传送门 题意 将n个数划分为两块,最多改变一个数的位置, 问能否使两块和相等 分析 因为我们最多只能移动一个数x,那么要么将该数往前移动,要么往后移动,一开始处理不需要移动的情况 那么遍历sum[i] ...
- 【multimap的应用】D. Array Division
http://codeforces.com/contest/808/problem/D #include<iostream> #include<cstdio> #include ...
- codeforces 808 D. Array Division(二分)
题目链接:http://codeforces.com/contest/808/problem/D 题意:有一串长度为n的数组,要求选择一个数字交换它的位置使得这串数能够分成两串连续的和一样的数组. 这 ...
- Codeforces D. Array Division
题目链接:http://codeforces.com/contest/808/problem/D 题意: 这一题给你一个数组,你可以调换某一个数的位置,使得这个数组可以分成2半,前半段的和等于后半段( ...
- CF808D STL
D. Array Division time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
随机推荐
- TMG2010安装配置细节设定
TMG2010最适合的操作系统是Win2008R2,不支持Win2012,可能是因为发布Win2012系统时,微软已经决定废弃TMG改为支持UAG了吧. 在Win2012下安装TMG2010,运行TM ...
- swift中UIImageView的创建
let imageView = UIImageView() let imageView1 = UIImageView(frame: CGRectMake(, , , )) // 创建出来的UIImag ...
- DataTable--数据生成datatable
将数据库查出的数据生成datatable 我们一般将数据库查询出的数据用实体接受在泛型集合,然后遍历集合,以将数据绑定到前台展示,在很多情况下,泛型集合不如datatable更方便将数据操作,这里简单 ...
- php loop循环 拿到键名
{loop $nianjian['dache'] $key $v} $key能拿到键名(微擎系统试过可以)
- Bootstrap 代码
[Bootstrap 代码] Bootstrap 允许您以两种方式显示代码: 第一种是 <code> 标签.如果您想要内联显示代码,那么您应该使用 <code> 标签. 第二种 ...
- JMeter学习(十七)JMeter测试MongoDB(转载)
转载自 http://www.cnblogs.com/yangxia-test JMeter测试MongoDB性能有两种方式,一种是利用JMeter直接进行测试MongoDB,还有一种是写Java代码 ...
- springboot logback
/resources/logback-spring.xml <configuration> <appender name="stdout" class=" ...
- 【Django】ModuleNotFoundError: No module named 'books_ordersschool'
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x00000 ...
- 虚拟机VM下CentOS7部署WASND9+HTTP9
本地操作系统:WIN10(64位) 虚拟机:14.1.1 build-7528167 安装环境:CentOS7 64 软件版本:IM 1.8 WAS 9.0+HTTP9.0 一.安装CentOS7 ...
- python爬虫相关基础概念
什么是爬虫 爬虫就是通过编写程序模拟浏览器上网,然后让其去互联网上抓取数据的过程. 哪些语言可以实现爬虫 1.php:可以实现爬虫.但是php在实现爬虫中支持多线程和多进程方面做得不好. 2.java ...