Codeforces 808D. Array Division
题目大意
给定你一个长为\(n\)的序列,问能否在最多一次取出某一元素然后插入到某一点后可以将整个序列分成两段使得其两段的元素之和相同.
\(n \leq 10^5\)
题解
发现插入操作实际上是让某一个元素与端点周围的元素交换。
维护一个支持插入和查找元素是否存在的ds即可.
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(ll &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
#define rg register int
#define rep(i,a,b) for(rg i=(a);i<=(b);++i)
#define per(i,a,b) for(rg i=(a);i>=(b);--i)
const int maxn = 100010;
ll a[maxn],pre[maxn],suf[maxn];
multiset<ll>S;
int main(){
ll n;read(n);
rep(i,1,n) read(a[i]),pre[i] = pre[i-1] + a[i];
per(i,n,1) suf[i] = suf[i+1] + a[i];
rep(i,1,n){
S.insert(a[i]);
if(pre[i] == suf[i+1]){
puts("YES");
return 0;
}
ll x = pre[i] - suf[i+1] + 2LL*a[i+1];
if(x&1) continue;
x >>= 1;
if(S.count(x) != 0){
puts("YES");
return 0;
}
}
S.clear();
per(i,n,1){
S.insert(a[i]);
ll x = suf[i] - pre[i-1] + 2LL*a[i-1];
if(x&1) continue;
x >>= 1;
if(S.count(x) != 0){
puts("YES");
return 0;
}
}
puts("NO");
return 0;
}
Codeforces 808D. Array Division的更多相关文章
- Codeforces D. Array Division
题目链接:http://codeforces.com/contest/808/problem/D 题意: 这一题给你一个数组,你可以调换某一个数的位置,使得这个数组可以分成2半,前半段的和等于后半段( ...
- 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 808D
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 808 D. Array Division(二分)
题目链接:http://codeforces.com/contest/808/problem/D 题意:有一串长度为n的数组,要求选择一个数字交换它的位置使得这串数能够分成两串连续的和一样的数组. 这 ...
- Educational Codeforces Round 21 D - Array Division (前缀和+二分)
传送门 题意 将n个数划分为两块,最多改变一个数的位置, 问能否使两块和相等 分析 因为我们最多只能移动一个数x,那么要么将该数往前移动,要么往后移动,一开始处理不需要移动的情况 那么遍历sum[i] ...
- 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 ...
- Codeforces 797E - Array Queries
E. Array Queries 题目链接:http://codeforces.com/problemset/problem/797/E time limit per test 2 seconds m ...
- Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力
Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...
随机推荐
- linux练习命令
任务一:按要求完成以下操作1)显示日期格式2)在/tmp/下新建目录test ,并指定权限6643)显示环境变量path,但将/root加入到$PATH中4)用cat显示/etc/passwd,并打印 ...
- 【leetcode刷题笔记】Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 防止iframe被别的网站引用
try{ top.location.hostname; if (top.location.hostname != window.location.hostname) { top.location.hr ...
- CSS3透明背景表单
在线演示 本地下载
- 乌云TOP 10 简单介绍
已知OWASP TOP10的WEB漏洞,乌云出了一个更加符合中国国情的 乌云:Top10 for 2014. A1-互联网泄密事件/撞库攻击 本质上来说是使用了不安全的口令,也许我可以将自己的密码设置 ...
- crm销售管理系统一
一.环境配置 1.首先配置pip,环境变量配置 pip 9.0.1 from c:\users\administrator\envs\crm\lib\site-packages (python 3.6 ...
- springmvc-restful
1.restful概述 REST 仅仅是一种架构的风格,并不是真正的架构,也不是一个软件,而是一种思想. 我们可以基于现有的HTTP.URI.XML.等现有技术来实现REST的风格.而不用去学习任何新 ...
- MySQL操作的相关命令
拷贝表,并且复制两条数据到新表中 create table t_comments_sample2 like t_comments_sample; #拷贝表结构 ,;#复制两条数据 MySQL Work ...
- ios点击事件失效
当使用委托给一个元素添加click事件时,如果事件是委托到 document 或 body 上,并且委托的元素是默认不可点击的(如 div, span 等),此时 click 事件会失效. 解决办法有 ...
- mini6410基于linux2.6.36内核通过NFS启动根文件系统总结(四制作根文件系统及通过NFS挂载文件系统)
http://blog.csdn.net/yinjiabin/article/details/7489563 根文件系统一般包括: 1)基本的文件系统结构,包含一些必须的目录,比如:/dev,/pro ...