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 ...
随机推荐
- pyhton3 re模块
本文转自 AstralWind 的博客:Python正则表达式指南 特来收藏 1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有 ...
- $Android设置TextView的字体
做项目的时候,需要使用到手写字体来让内容更加的美观.可是程序中默认使用的是系统的默认字体,怎么将TextView(或EditText)的字体设置成自己想要的字体呢?步骤如下: 1.下载字体文件(.tt ...
- pdoModel封装
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2017/7/24 * Time: 14:03 */ /** * 数 ...
- 解决Ubuntun 12.04编译Mesa10.3 WARNING: 'aclocal-1.14' is missing on your system
安 装Mesa时,最后一个错误报“WARNING: 'aclocal-1.14' is missing on your system.”,虽然是个Warning,但是无法进行下一步make,所以必须解 ...
- js怎么将光标移动特定的位置:
第一种方法: a 标签的锚: 将a标签的herf='#element_id_name' 即可 <a href="#comment_content" class=" ...
- Java中的HashMap
今天到中关村软件园面试被问到Java中HashMap的存值原理,瞬间无言已对,回答用了一个数组,然后沉默,面试官说,一次的面试失败不算什么,之后...... 1.关于hashCode hashCode ...
- CSS3动画表单
在线演示 本地下载
- maven项目在打war包时出现非法字符: '\ufeff' 解决方案
问题描述: 开发工具MyEclipse 的总体开发环境,编码格式总体设置为UTF-8,在将web项目打包的时候出现:非法字符:'\ufeff" 错误. 解决方案: 利用notePad++打开 ...
- Linux下检测IP访问特定网站的ruby脚本
root@ubuntu:~# vi check_ip.rbrequire 'rubygems' index = 1 max = 20 while (max-index) >= 0 puts in ...
- JSP--内置对象&动作标签介绍
1.JSP中常用的9大内置对象? 内置对象:在JSP页面中能直接使用的对象就是JSP内置对象,事实上,JSP底层就是一个java类,可以在JSP中直接使用的,必然存在JSP翻译后的java类 下面简单 ...