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 ...
随机推荐
- poj 1694 An Old Stone Game 树形dp
//poj 1694 //sep9 #include <iostream> #include <algorithm> using namespace std; const in ...
- Html5培训之精髓
一.核心技术(可去各技术官网学习) 1.html5的六大核心技术:Html5,CSS3,JavaScript,WebSocket,PhoneGap,Node.js,它们覆盖了设备端,浏览器端和云端的开 ...
- postgres源码目录结构
1.第一级目录介绍 |_postgres |_aclocal.m4------------config用的文件的一部分 |_config----------------config文件目录 |_con ...
- XMLHTTPRequest DEMO(发送测试)
对于其中的HTTP状态,我们知道200-299表明访问成功:300-399表明需要客户端 反应来满足请求:400-499和500-599表明客户端和服务器出错:其中常用的如404表示资源没找到,403 ...
- MyCAT简单入门配置
MyCAT简单入门配置 安装jdk 建议1.7以上 安装mysql 安装MyCAT Mycat 源码:https://github.com/MyCATApache/Mycat-Server Mycat ...
- Mongo.setReadPref(mode, tagSet) primaries and secondaries are treated equivalently. 读优先级策略
https://docs.mongodb.com/manual/reference/method/Mongo.setReadPref/#Mongo.setReadPref Mongo.setReadP ...
- 一个简单的servlet
1.创建一个自己的servlet文件,继承HttpServlet MyServlet.java package com.jmu.ccjoin.controller; import java.io.IO ...
- 解决ubuntu没有/var/log/messages的问题
1:root身份打开 /etc/rsyslog.d/50-default.conf 2:把注释#去掉 #*.=info;*.=notice;*.=warn;\ # auth,authpriv.none ...
- django-sso单点登陆的实现
环境准备 环境规格: python3.5 django2.0 django-simple-sso-0.14 环境安装: pip install django-simple-sso-0.14 环境说明: ...
- WAS域名解析问题
1.如果dmgr和his在一台机器上,但web服务器用的是app服务器 如下图: 插件都处理完成. 这时候,通过外网域名访问时,出现如下情况 或者 说明:ihs服务器上webserver1的文件Plu ...