Educational Codeforces Round 21 D - Array Division (前缀和+二分)
传送门
题意
将n个数划分为两块,最多改变一个数的位置,
问能否使两块和相等
分析
因为我们最多只能移动一个数x,那么要么将该数往前移动,要么往后移动,一开始处理不需要移动的情况
那么遍历sum[i]
如果往前移动,sum[k]+(sum[i]-sum[i-1])=sum[n]/2,k∈[1,i-1]
如果往后移动,sum[k]-(sum[i]-sum[i-1])=sum[n]/2,k∈[i+1,n]
一开始我没有考虑往后移动
时间复杂度\(O(nlog(n))\)
trick
代码
#include<cstdio>
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
int n;
ll sum[100100];
ll ans,x;
bool find(int l,int r,ll val)
{
while(l<=r)
{
int mid=(l+r)/2;
if(val<sum[mid]) r=mid-1;
if(val>sum[mid]) l=mid+1;
if(val==sum[mid]) return 1;
}
return 0;
}
int flag=0;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%lld",&x);
sum[i]=sum[i-1]+x;
}
if(sum[n]&1) { puts("NO");return 0; }
ans=sum[n]/2;
//if(find(1,n,ans)) {flag=1;goto loop;}
for(int i=1;i<=n;++i)
{
ll ret=sum[i]-sum[i-1]+ans;
if(find(i+1,n,ret)) { flag=1;break; }
ret=ans-(sum[i]-sum[i-1]);
if(find(1,i-1,ret)) { flag=1;break; }
}
//if(find(1,n-2,sum[n-1]-ans)) flag=1;
loop:
flag?puts("YES"):puts("NO");
}
Educational Codeforces Round 21 D - Array Division (前缀和+二分)的更多相关文章
- 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 ...
- 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 11 C. Hard Process 前缀和+二分
题目链接: http://codeforces.com/contest/660/problem/C 题意: 将最多k个0变成1,使得连续的1的个数最大 题解: 二分连续的1的个数x.用前缀和判断区间[ ...
- Educational Codeforces Round 21
Educational Codeforces Round 21 A. Lucky Year 个位数直接输出\(1\) 否则,假设\(n\)十进制最高位的值为\(s\),答案就是\(s-(n\mod ...
- 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 61 C 枚举 + 差分前缀和
https://codeforces.com/contest/1132/problem/C 枚举 + 差分前缀和 题意 有一段[1,n]的线段,有q个区间,选择其中q-2个区间,使得覆盖线段上的点最多 ...
- Educational Codeforces Round 21 Problem E(Codeforces 808E) - 动态规划 - 贪心
After several latest reforms many tourists are planning to visit Berland, and Berland people underst ...
- Educational Codeforces Round 21 Problem A - C
Problem A Lucky Year 题目传送门[here] 题目大意是说,只有一个数字非零的数是幸运的,给出一个数,求下一个幸运的数是多少. 这个幸运的数不是最高位的数字都是零,于是只跟最高位有 ...
- 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 ...
随机推荐
- leetcode Valid Palindrome C++&python 题解
题目描写叙述 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- java中简单字符替换
在网络编程中,假设URL含有特殊字符,如空格.'#'等,server将无法识别导致无法获得正确的參数值.我们须要将这些特殊字符转换成server能够识别的字符,比如将空格转换成'%20'.给定一个字符 ...
- google protocol buffer的原理和使用(一)
一.简单的介绍 Protocol buffers是一个用来序列化结构化数据的技术,支持多种语言诸如C++.Java以及Python语言.能够使用该技术来持久化数据或者序列化成网络传输的数据. ...
- C开发人员眼中的SICP学习
谈谈自己看SICP的一些体会 第一章 构造过程抽象 这一章事实上和C语言全然等价, 不打算深入学习LISP的能够高速略过. 思想上没有太多新的东西. 这一章最核心的价值就是以下3句话, 理解了这一章 ...
- 宠物连连看2完整Android代码项目
宠物连连看2完整代码,该源代码支持多种风格的连连看游戏的,如有国旗类的连连看,还有宠物连连看的等,主要的功能实现了无尽关卡挑战模式.还有催命倒计时,以及链接提示,暂停.多样图片集,挑战眼力和速度等,而 ...
- 利用easyUI的combobox打造自己主动提示组件
自己主动提示是时下一个非常流行的功能,比方说百度.谷歌的搜索输入框都使用到了这么一个功能. 因为easyUI的combobox设计师已经考虑到了这个功能.所以仅仅需简单几步我们能够轻松打造自己的自己主 ...
- LeetCode(28)题解:Implement strStr()
https://leetcode.com/problems/implement-strstr/ 题目: Implement strStr(). Returns the index of the fir ...
- CentOS笔记-其他杂记
1.忘记密码时,可以用single模式来修改密码,为了安全,可以禁用single模式,参考网址如下 Centos服务器禁止单用户模式(single)来增强系统安全 2.远程登录:ssh root@xx ...
- button在firefox 和 ie 下的问题
最近做了一个关于数据库管理的项目,因为不用考虑ie9以下的兼容性,所以一股脑的写完啦,到测试的时候发现了一个bug IE和火狐下有个模块关闭按钮的hover没有反应,ie不行就算了,火狐怎么也不行?我 ...
- 使用a标签下载文件,而不是直接打开,使用属性 download
有的时候,下载的链接文件如果是普通文件类型,如txt,我们下载文件的时候,有的浏览器不会弹出下载框,.而是直接打开了该文件. 针对这种情况,我们只需要在a标签上加上download属性即可显示下载框. ...