Educational Codeforces Round 21 D.Array Division(二分)
D. Array Division
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.
题目链接:http://codeforces.com/contest/808/problem/D
题意:在数组中移动一个数 使得分组可以分割成两个数组 使得两个数组之和相等
分析:
首先分两种情况
1. 往后面移动一个数到前面 (维护前缀和 看看后面有没有符合条件的数)
2. 移掉一个数 (移掉第i个数 看看连续的数能不能符合条件)
用二分做,观摩观摩
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=;
ll s[N],a[N],sum;
int n;
bool find(int l,int r,ll x)
{
while(l<=r)
{
int mid=(l+r)/;
if(s[mid]==x)
return ;
if(s[mid]<x)
l=mid+;
else r=mid-;
}
return ;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
sum+=a[i];
s[i]=s[i-]+a[i];//求前缀和
}
if(sum&)
{
cout<<"NO"<<endl;
return ;
}
sum/=;
for(int i=;i<=n;i++)
{
if(find(i+,n,sum+a[i])||find(,i-,sum-a[i]))
{
cout<<"YES"<<endl;
return ;
}
}
cout<<"NO"<<endl;
return ;
}
Educational Codeforces Round 21 D.Array Division(二分)的更多相关文章
- Educational Codeforces Round 21 D - Array Division (前缀和+二分)
传送门 题意 将n个数划分为两块,最多改变一个数的位置, 问能否使两块和相等 分析 因为我们最多只能移动一个数x,那么要么将该数往前移动,要么往后移动,一开始处理不需要移动的情况 那么遍历sum[i] ...
- Educational Codeforces Round 21
Educational Codeforces Round 21 A. Lucky Year 个位数直接输出\(1\) 否则,假设\(n\)十进制最高位的值为\(s\),答案就是\(s-(n\mod ...
- Educational Codeforces Round 21 Problem F (Codeforces 808F) - 最小割 - 二分答案
Digital collectible card games have become very popular recently. So Vova decided to try one of thes ...
- 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 ...
- Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process 题目连接: http://www.codeforces.com/contest/660/problem/C Description You are given an a ...
- Educational Codeforces Round 26 F. Prefix Sums 二分,组合数
题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...
- Educational Codeforces Round 11A. Co-prime Array 数学
地址:http://codeforces.com/contest/660/problem/A 题目: A. Co-prime Array time limit per test 1 second me ...
- Educational Codeforces Round 21(A.暴力,B.前缀和,C.贪心)
A. Lucky Year time limit per test:1 second memory limit per test:256 megabytes input:standard input ...
- Educational Codeforces Round 21 Problem E(Codeforces 808E) - 动态规划 - 贪心
After several latest reforms many tourists are planning to visit Berland, and Berland people underst ...
随机推荐
- Struts2学习---基本配置,action,动态方法调用,action接收参数
首先我们先来直接配置,然后再来讲原理: 第一步:jar包的引入: 我们可以到struts2的官网上下载: http://struts.apache.org/download.cgi#struts251 ...
- iOS App稳定性指标及监测
一个App的稳定性,主要决定于整体的系统架构设计,同时也不可忽略编程的细节,正所谓"千里之堤,溃于蚁穴",一旦考虑不周,看似无关紧要的代码片段可能会带来整体软件系统的崩溃.尤其因为 ...
- geoserver发布地图服务WMS
wms服务发布: 1.打开geoserver管理首页(网址为http://localhost:8080/geoserver/web/),并使用安装时设置的帐户名和密码登录(这里是admin/geose ...
- su 和 sudo 命令的区别-转载
link 一. 使用 su 命令临时切换用户身份 1.su 的适用条件和威力 su命令就是切换用户的工具,怎么理解呢?比如我们以普通用户beinan登录的,但要 ...
- (一)初识mybatis
Mybatis 是现在很多公司都选择使用的一个ORM(Object Relational Mapping)框架,所以是值得了解和学习一番的. MyBatis 是支持定制化 SQL.存储过程以及高级映射 ...
- 实体框架(Entity Frmaework)简介
l简称EF NH l与Asp.Net MVC关系与ADO.NET关系 lADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapp ...
- VS2017 调试期间无法获取到变量值查看
只要把勾去掉就能查看变量的值了
- bash脚本之数组学习
在bash中可使用索引数组和关联数组,bash在4.0版本之后才添加了对关联数组的支持 一.索引数组 1.定义索引数组 # 方式1 array_value=(1 2 3 4 5 6)或者array_v ...
- ASP.NET Core学习之二 菜鸟踩坑
对于像我这样没接触过core的人,坑还是比较多的,一些基础配置和以前差别很大,这里做下记录 一.Startup 1.注册服务 // This method gets called by the run ...
- 纯前端导出pdf文件
纯前端js导出pdf,已经用于生产环境. 工具: 1.html2canvas,一种让html转换为图片的工具. 2.pdfmake或者jspdf ,一种生成.编辑pdf,并且导出pdf的工具. pdf ...