题目链接:http://codeforces.com/contest/808/problem/D

题意:有一串长度为n的数组,要求选择一个数字交换它的位置使得这串数能够分成两串连续的和一样的数组。

这个数还可以和自己交换位置。

题解:很显然求一下前缀二分每个数看一下能否插入,再求一下后缀二分每个数看一下能否插入。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int M = 1e5 + 10;
ll a[M] , be[M] , af[M];
bool binsearch(int l , int r , ll sum , ll gg[] , int pos) {
int mid = (l + r) >> 1;
while(l <= r) {
mid = (l + r) >> 1;
if(gg[mid] == sum) {
if(pos == mid) return false;
else return true;
}
if(gg[mid] > sum) r = mid - 1;
if(gg[mid] < sum) l = mid + 1;
}
return false;
}
bool binsearch2(int l , int r , ll sum , ll gg[] , int pos) {
int mid = (l + r) >> 1;
while(l <= r) {
mid = (l + r) >> 1;
if(gg[mid] == sum) {
if(pos == mid) return false;
else return true;
}
if(gg[mid] > sum) l = mid + 1;
if(gg[mid] < sum) r = mid - 1;
}
return false;
}
int main() {
int t;
scanf("%d" , &t);
for(int i = 1 ; i <= t ; i++) {
scanf("%lld" , &a[i]);
}
ll sum = 0;
for(int i = 1 ; i <= t ; i++) {
sum += a[i];
}
if(sum % 2 != 0) {
printf("NO\n");
}
else {
be[0] = 0;
for(int i = 1 ; i <= t ; i++) {
be[i] = be[i - 1] + a[i];
}
af[t + 1] = 0;
for(int i = t ; i >= 1 ; i--) {
af[i] = af[i + 1] + a[i];
}
ll aim = sum / 2;
int flag = 0;
for(int i = 1 ; i <= t ; i++) {
if(be[i] == aim) {flag = 1; break;}
}
for(int i = 1 ; i <= t ; i++) {
if(flag) break;
bool temp = binsearch(0 , i , aim - a[i] , be , i);
if(temp) {flag = 1; break;}
temp = binsearch2(i , t + 1 , aim - a[i] , af , i);
if(temp) {flag = 1; break;}
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}

codeforces 808 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. Codeforces 482B Interesting Array(线段树)

    题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...

  3. Codeforces 1077C Good Array 坑 C

    Codeforces 1077C Good Array https://vjudge.net/problem/CodeForces-1077C 题目: Let's call an array good ...

  4. Array Division 808D

    D. Array Division time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  5. codeforces 482B. Interesting Array【线段树区间更新】

    题目:codeforces 482B. Interesting Array 题意:给你一个值n和m中操作,每种操作就是三个数 l ,r,val. 就是区间l---r上的与的值为val,最后问你原来的数 ...

  6. codeforces 407C Curious Array

    codeforces 407C Curious Array UPD: 我觉得这个做法比较好理解啊 参考题解:https://www.cnblogs.com/ChopsticksAN/p/4908377 ...

  7. codeforces 797 E. Array Queries【dp,暴力】

    题目链接:codeforces 797 E. Array Queries   题意:给你一个长度为n的数组a,和q个询问,每次询问为(p,k),相应的把p转换为p+a[p]+k,直到p > n为 ...

  8. Educational Codeforces Round 21 D - Array Division (前缀和+二分)

    传送门 题意 将n个数划分为两块,最多改变一个数的位置, 问能否使两块和相等 分析 因为我们最多只能移动一个数x,那么要么将该数往前移动,要么往后移动,一开始处理不需要移动的情况 那么遍历sum[i] ...

  9. codeforces 808 E. Selling Souvenirs (dp+二分+思维)

    题目链接:http://codeforces.com/contest/808/problem/E 题意:最多有100000个物品最大能放下300000的背包,每个物品都有权值和重量,为能够带的最大权值 ...

随机推荐

  1. 【python-Django开发】Django 配置MySQL数据库讲解!!!

    官方文档请阅读:https://docs.djangoproject.com/en/1.11/ref/databases/#mysql-db-api-drivers 配置MySQL数据库 1. 新建M ...

  2. 2019前端面试系列——HTTP、浏览器面试题

    浏览器存储的方式有哪些 特性 cookie localStorage sessionStorage indexedDB 数据生命周期 一般由服务器生成,可以设置过期时间 除非被清理,否则一直存在 页面 ...

  3. mysql新增一列为主键

    mysql新增一列为主键 由于一次疏忽在建表的时候忘记加上主键了, 但是目前来说表里面又有数据了,所以不能删表重建,所以需要新加一列主键 然后我就新加一列,并且为auto_increment,然后设置 ...

  4. C#下载文件,Stream 和 byte[] 之间的转换

    stream byte 等各类转换 http://www.cnblogs.com/warioland/archive/2012/03/06/2381355.html using (System.Net ...

  5. CEPH 对象存储的系统池介绍

    RGW抽象来看就是基于rados集群之上的一个rados-client实例. Object和pool简述 Rados集群网上介绍的文章很多,这里就不一一叙述,主要要说明的是object和pool.在r ...

  6. 前端本地proxy跨域代理配置

    等了好久的接口,总算拿到了,结果却发现用本地localhost:9712去请求接口的时候,出现了跨域错误,而这个时候我们就需要进行下跨域配置了. 首先,找到项目中名为webpack.config.js ...

  7. L1005矩阵取数游戏

    #include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for ( ...

  8. 解决VS2008,重新生成解决方案,很慢

    正所谓:“工欲善其事,必先利其器“.我也算是深受其害了,特把经验分享出来为大伙分忧! 在刚来公司的时候,使用的公司提供的VS2008作为开发工具,有一个非常让人不爽的问题,就是在重新编译代码(重新生成 ...

  9. 主成分分析 Principle Component Analysis

    一.主要思想 利用正交变换把可能线性相关变量表示的观测数据,转换为由少数几个线性无关变量(主成分)表示的数据.(重构原始特征空间:线性降维) 要尽可能保留原始数据中的信息,两个思路:最大投影方差.最小 ...

  10. android——卡片式布局

    一.CardView <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk ...