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 ...
随机推荐
- idea git 发起一个pull request 请求
- Python自动化运维开发实战 三、python文件类型
导语: python常用的有3种文件类型 1. 源代码 py 2. 字节代码 pyc 3. 优化代码 pyo 源代码: python源代码的文件以”py"为扩展名,由python程序解释,不 ...
- MongoDB应用场景
数据记录如下 /* 1 */ { "_id" : ObjectId("5b56dd19a171d7e9bfb03ac1"), "name" ...
- CSS样式表的写作规范
推荐大家使用的CSS书写规范.顺序 写了这么久的CSS,但自己都没有按照良好的CSS书写规范来写CSS代码,东写一段西写一段,命名也是想到什么写什么,过一段时间自己都不知道写的是那一块内容, 这样会影 ...
- CSS----学习2
CSS2属性 文本 1 水平对齐方式 text-align:left/right/center 也可以让img.input等有水平方向的对齐方式 2 垂直对齐方式 vertical-align:top ...
- keras初探
1.对网络的理解: 2.怎样训练,输入已经数据,经过训练,输入测试数据,得到相似数据 3.RNNs 循环神经网络
- Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag.
https://blog.csdn.net/watermusicyes/article/details/44963773 Context中有一个startActivity方法,Activity继承自C ...
- 工单进入IN_MO后在FP_PREPROCESS被过滤
'; --BOM and item not in IN_ITEMBOMROUTING SELECT * FROM TEMP_REMOVED_IN_DATA WHERE TABLE_NAME='IN_M ...
- jQuery权威指南(第2版) 学习一 jQuery操作DOM
jQuery操作DOM 获取元素的属性 attr(name) 获取元素属性的语法格式如下: attr(name) 其中,参数 name 表示属性的名称. 例子: <img alt="& ...
- python 自动补全
一.查看python 环境变量 >>> import sys>>> sys.path 编写 tab.py import sys import atexit impo ...