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 ...
随机推荐
- SNMP学习笔记之SNMP 原理与实战详解
原文地址:http://freeloda.blog.51cto.com/2033581/1306743 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法 ...
- WebService(JAX-WS、XFire、Axis三种)获取客户端ip
WebService(JAX-WS.XFire.Axis三种)获取客户端ip JAX-WS.XFire.Axis三种webservice的获取客户端IP的简单实现过程: 1,基于JDK6 jax-ws ...
- 01: Centos7 常用命令
1.1 centos7中防火墙相关命令 1.查看状态 1. getenforce # 查看内核防火墙状态(disabled标识关闭) 2. systemctl status f ...
- 七个月学习Python大计
仅以此篇纪念学习Python征程的开始
- 再谈树---无根树转有根树( dfs搜索转化+fa数组记录父节点) *【模板】
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <vector> ...
- 试着用React写项目-利用react-router解决跳转路由等问题(二)
转载请注明出处:王亟亟的大牛之路 这一篇还是继续写react router相关的内容,废话之前先安利:https://github.com/ddwhan0123/Useful-Open-Source- ...
- sql 之优化小技巧
SET NOCOUNT ON:不返回计数,如果存储过程中包含一些并不返回实际数据的语句,网络通信流量便会大量减少,可以显著提高应用程序性能:
- JavaScript权威指南2.词法结构
字符集 1.用16位的Unicode字符集编写的,可以表示地球上通用的每一种书面语言.国际化 2.每个字符都是用两个字节表示的 3.大小写敏感:关键字.变量.函数名.标识符:HTML并不区分大小写 H ...
- Cent OS 常用配置命令
1.ifconfig #查看网络接口状态 2.ifconfig –a #查看主机所有接口的情况 3.ifconfig eth0 192.168.1.106 netmask 255.255.255 ...
- echart折线图,柱状图,饼图设置颜色
转载: 之前在做报表的时候用过echart 用完也就完了,而这次在用的时候已经忘了,所以这里简单记录一下,好记性不如烂笔头!!! 1.折线图修改颜色: xAxis: { type: 'category ...