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 ...
随机推荐
- 吴裕雄 python深度学习与实践(1)
#coding = utf8 import threading,time count = 0 class MyThread(threading.Thread): def __init__(self,t ...
- python3编译安装
linux下配置安装python3一.首先,官网下载python3的所需版本.wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz ...
- servlet、servlet容器和web应用程序的关系
- centos 安装python3.6
环境准备 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel 首先去官网下 ...
- tensorflow serving 之minist_saved_model.py解读
最近在学习tensorflow serving,但是就这样平淡看代码可能觉得不能真正思考,就想着写个文章看看,自己写给自己的,就像自己对着镜子演讲一样,写个文章也像自己给自己讲课,这样思考的比较深,学 ...
- Python基础学习Day2
一.格式化输出 需求格式化输出:姓名.年龄.工作.爱好 # 格式化输出 name = input('请输入用户名:') age = input('请输入年龄:') job = input('请输入你的 ...
- js高级-模块化演变
function demo(){ var a = b = c = 9; // b,c全局变量 a局部变量 } demo(); console.log(b) 命名空间 var Shop = {} //顶 ...
- php正则替换双引号里面的字符
- Building and using plug-ins for Android
[Building and using plug-ins for Android] 1.AAR plug-ins and Android Libraries Android Archive (AAR) ...
- Python基础之-----------函数
---恢复内容开始--- 函数:在其他的语言中,我们也经常听到函数的概念,那么什么是函数呢?在Java中叫做method: 定义:函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函 ...