传送门

题意

将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 (前缀和+二分)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Educational Codeforces Round 11 C. Hard Process 前缀和+二分

    题目链接: http://codeforces.com/contest/660/problem/C 题意: 将最多k个0变成1,使得连续的1的个数最大 题解: 二分连续的1的个数x.用前缀和判断区间[ ...

  4. Educational Codeforces Round 21

    Educational Codeforces Round 21  A. Lucky Year 个位数直接输出\(1\) 否则,假设\(n\)十进制最高位的值为\(s\),答案就是\(s-(n\mod ...

  5. 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 ...

  6. Educational Codeforces Round 61 C 枚举 + 差分前缀和

    https://codeforces.com/contest/1132/problem/C 枚举 + 差分前缀和 题意 有一段[1,n]的线段,有q个区间,选择其中q-2个区间,使得覆盖线段上的点最多 ...

  7. Educational Codeforces Round 21 Problem E(Codeforces 808E) - 动态规划 - 贪心

    After several latest reforms many tourists are planning to visit Berland, and Berland people underst ...

  8. Educational Codeforces Round 21 Problem A - C

    Problem A Lucky Year 题目传送门[here] 题目大意是说,只有一个数字非零的数是幸运的,给出一个数,求下一个幸运的数是多少. 这个幸运的数不是最高位的数字都是零,于是只跟最高位有 ...

  9. 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 ...

随机推荐

  1. lua 获取当前执行目录 lfs 库

    lua lfs 库 lfs.attributes(filepath [, aname]) 获取路径指定属性 lfs.chdir(path) 改变当前工作目录,成功返回true,失败返回nil加上错误信 ...

  2. Android4.42-Setting源代码分析之蓝牙模块Bluetooth(下)

    接着上一篇Android4.42-Settings源代码分析之蓝牙模块Bluetooth(上) 继续蓝牙模块源代码的研究 THREE.蓝牙模块功能实现 switch的分析以及本机蓝牙重命名和可见性的分 ...

  3. js 验证 输入值 全是数字

    1.使用isNaN()函数 isNaN()的缺点就在于 null.空格以及空串会被按照0来处理 NaN: Not a Number /** *判断是否是数字 **/ function isRealNu ...

  4. 【转载】关于Hash

    这个HASH算法不是大学里数据结构课里那个HASH表的算法.这里的HASH算法是密码学的基础,比较常用的有MD5和SHA,最重要的两条性质,就是不可逆和无冲突.所谓不可逆,就是当你知道x的HASH值, ...

  5. 【转载】.NET Remoting学习笔记(三)信道

    目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 参考:♂风车车.Net .NET Framework ...

  6. BIOS维修技术

    BIOS是电脑中最基础且最重要的程序,为电脑提供最低级且最直接的硬件控制,电脑的原始操作都是依照固化在BIOS里的程序来完成的.因此如果BIOS出现故障将会导致影响电脑的正常工作.BIOS故障有很多, ...

  7. region split流程分析

    region split流程分析 splitregion的发起主要通过client端调用regionserver.splitRegion或memstore.flsuh时检查并发起. Client通过r ...

  8. Anaconda和Pycharm安装和配置教程

    1.下载Anaconda2 (最好选Python2.7的,兼容性好点)             在官网下载:https://www.continuum.io/downloads 2.安装Pycharm ...

  9. Mac版的idea部分按钮失效的解决方案

    问题描述:调整了一下idea中jdk的路径,之后idea就无法打开新项目了,最好发现idea中的顶部菜单全部失效 解决过程: 1.把idea的jdk的路径调回去,无效 2.重启idea,无效 3.重启 ...

  10. Phoenix put the sql back in NoSql

    Overview | Apache Phoenix http://phoenix.apache.org/index.html Apache Phoenix enables OLTP and opera ...