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 ...
随机推荐
- iOS --- 总结Objective-C中经常使用的宏定义(持续更新中)
将iOS开发中经常使用的宏定义整理例如以下,仅包括Objective-C. 而对于Swift,不能使用宏,则能够定义全局函数或者extension.请參考博客iOS - 总结Swift中经常使用的全局 ...
- js跳出循环的方法区别( break, continue, return ) 及 $.each 的(return true 和 return false)
js编程语法之break语句: break语句会使运行的程序立刻退出包含在最内层的循环或者退出一个switch语句. 由于它是用来退出循环或者switch语句,所以只有当它出现在这些语句时,这种形式的 ...
- webpack2 详解
1.安装 npm install webpack -g npm install webpack -save-dev 2.编辑配置文件 // 引入 path var path=require('path ...
- vimrc 避免中文乱码配置
et smsyntax onset ts=4set sts=4set sw=4set hlsearchset rulerset backspace=indent,eol,startset encodi ...
- android项目笔记(一)
1.getInstance:单例模式创建类的实例,getInstance在单例模式(保证一个类仅有一个实例,并提供一个访问它的全局访问点)的类中常见,用来生成唯一的实例,getInstance往往是s ...
- Go语言测试代码
第一次学go语言,测试代码 package main import "fmt" var age int; const sex = 0 func init() { fmt.Print ...
- poj 2264 Advanced Fruits(DP)
Advanced Fruits Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1944 Accepted: 967 ...
- Kafka知识点汇总
整体结构 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZXJpY19zdW5haA==/font/5a6L5L2T/fontsize/400/fill/I ...
- sanic官方文档解析之streaming(流动,滚动)和class_based_views(CBV的写法)
1,streaming(流媒体) 1.1请求流媒体 Sanic允许你通过流媒体携带请求数据,如下,当请求结束await request.stream.read()就会返回None,仅仅只有post请求 ...
- HDU Shell Necklace CDQ分治+FFT
Shell Necklace Problem Description Perhaps the sea‘s definition of a shell is the pearl. However, in ...