CF808D STL
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.
从一个数列中至多移动一个数字插至任意位置,问是否有可能使得数列可以被分为相等的两部分.
一开始用的map<int,pair<int,int> > 虽然A了可后来发现map的话不能判重会出错的啊,果然被hack了,
用一个map<LL,int> 表示某一个数出现的次数然后模拟即可
#include<bits/stdc++.h>
using namespace std;
#define LL long long
int a[100005];
map<LL,int> M;
int main()
{
int n,i,j,k;
LL sumn=0,ans;
cin>>n;
for(i=1;i<=n;++i){
scanf("%d",a+i);
sumn+=a[i];
++M[a[i]];
}
if(sumn & 1) {
puts("NO");
return 0;
}
LL tmp=sumn/2;
if(M[tmp]>0) {puts("YES");return 0;}
for(i=1;i<=n;++i){
tmp-=a[i];
--M[a[i]];
if(M[tmp]>0) {
puts("YES");
return 0;}
} tmp=sumn/2;
for(i=1;i<=n;++i) ++M[a[i]];
for(i=n;i>=1;i--){
tmp-=a[i];
--M[a[i]];
if(M[tmp]>0) {puts("YES");return 0;}
}
puts("NO");
return 0;
}
CF808D STL的更多相关文章
- 详细解说 STL 排序(Sort)
0 前言: STL,为什么你必须掌握 对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算 ...
- STL标准模板库(简介)
标准模板库(STL,Standard Template Library)是C++标准库的重要组成部分,包含了诸多在计算机科学领域里所常见的基本数据结构和基本算法,为广大C++程序员提供了一个可扩展的应 ...
- STL的std::find和std::find_if
std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if. 小例子: #include "stdafx.h" #i ...
- STL: unordered_map 自定义键值使用
使用Windows下 RECT 类型做unordered_map 键值 1. Hash 函数 计算自定义类型的hash值. struct hash_RECT { size_t operator()(c ...
- C++ STL简述
前言 最近要找工作,免不得要有一番笔试,今年好像突然就都流行在线笔试了,真是搞的我一塌糊涂.有的公司呢,不支持Python,Java我也不会,C有些数据结构又有些复杂,所以是时候把STL再看一遍了-不 ...
- codevs 1285 二叉查找树STL基本用法
C++STL库的set就是一个二叉查找树,并且支持结构体. 在写结构体式的二叉查找树时,需要在结构体里面定义操作符 < ,因为需要比较. set经常会用到迭代器,这里说明一下迭代器:可以类似的把 ...
- STL bind1st bind2nd详解
STL bind1st bind2nd详解 先不要被吓到,其实这两个配接器很简单.首先,他们都在头文件<functional>中定义.其次,bind就是绑定的意思,而1st就代表fir ...
- STL sort 函数实现详解
作者:fengcc 原创作品 转载请注明出处 前几天阿里电话一面,被问到STL中sort函数的实现.以前没有仔细探究过,听人说是快速排序,于是回答说用快速排序实现的,但听电话另一端面试官的声音,感觉不 ...
- STL的使用
Vector:不定长数组 Vector是C++里的不定长数组,相比传统数组vector主要更灵活,便于节省空间,邻接表的实现等.而且它在STL中时间效率也很高效:几乎与数组不相上下. #include ...
随机推荐
- jquery-easyui combobox combogrid 级联不可编辑实例
jquery-easyui combobox combogrid 级联不可编辑实例 如何让jquery-easyui的combobox像select那样不可编辑?为combobox添加editable ...
- Web安全学习笔记之DES算法实例详解
转自http://www.hankcs.com/security/des-algorithm-illustrated.html 译自J. Orlin Grabbe的名作<DES Algorith ...
- 关于微信分享到朋友圈(Thinkphp-tp3.2框架下实现)
PHP部分 扩展类代码部分: <?php namespace Think; class JsSdk { private $appId; private $appSecret; public $d ...
- centos/rhel 6.5(更新至centos 7)下rabbitmq安装(最简单方便的方式)
vim /etc/hosts 增加 127.0.0.1 hostname 不然启动的时候可能会报如下错误: [root@devel2 rabbitmq]# rabbitmq-server ERROR: ...
- Python3基础 函数 局部与全局变量同名,各管各的
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- hdu2552 (浮点数复杂运算的四舍五入)题解
三足鼎立 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- ACM-ICPC 2018 南京赛区网络预赛 E AC Challenge 状压DP
题目链接: https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest with n (0 < n \le 20)n(0& ...
- [不屈的复习] - http://how2j.cn/
http://how2j.cn/ 该教程网站分得比较规整!
- spark-shuffle分析
前言 shuffle是分布式计算系统中最重要的一部分,spark和mapreduce的shuffle的大体思路类似,在实现上有一些区分.Spark提供了插件式的接口,使用者可以通过继承ShuffleM ...
- LA 3027 合作网络
https://vjudge.net/problem/UVALive-3027 题意: 有n个结点,初始时每个结点的父节点都不存在.你的任务是执行一次I操作和E操作,格式如下: I u v:把结点u的 ...