Codeforces D. Array Division
题目链接:http://codeforces.com/contest/808/problem/D
题意:
这一题给你一个数组,你可以调换某一个数的位置,使得这个数组可以分成2半,前半段的和等于后半段(严格的前半段和后半段)。问你能不能构成。
题解:
一开始读题的时候,被吓坏了,没有看到是移动一个,因为题目在output那里才有写是移动一个。
那么如果这个和是奇数的话,就无法分成2个相等的部分。则是NO。
如果这个数列里有一个数是sum/2的话也是YES。
如果是移动一个数,那么这个数一定在某一个大于sum/2的前缀和里或者是在某一个大于sum/2的后缀和里面。因为大于sum/2的时候一定要移动一个数,才能是前缀和为sum/2。
同理从后面来看也是这样。
我们就可以用一个set来保存前面出现过的ai
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define eps 0.0000001
typedef long long LL;
typedef unsigned long long ULL;
const int inf = 0x3f3f3f3f;
const LL INF = 0x7fffffff;
const int maxn = 1e5+;
const int mod = 1e9+;
LL a[maxn];
void init()
{ }
void solve()
{
ios::sync_with_stdio(); int n;
LL sum = ;
cin >> n;
for(int i = ;i<=n;i++) cin >> a[i], sum+=a[i];
if(sum%==){
cout << "NO" << endl;
return;
}
sum /= ;
for(int i = ;i<=n;i++){
if(a[i] == sum) {
cout << "YES" << endl;
return;
}
}
LL x = ;
set<LL> S;
for(int i = ;i<=n;i++){
x += a[i];
S.insert(a[i]);
if(S.count(x - sum)){
cout << "YES" << endl;
return;
}
}
S.clear();
for(int i= n;i>;i--){
x -= a[i];
S.insert(a[i]);
if(S.count(sum-x)){
cout << "YES" << endl;
return;
}
}
cout << "NO" << endl;
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL
solve();
return ;
}
你努力的时候,比你厉害的人也在努力。
Codeforces D. Array Division的更多相关文章
- Codeforces 808D. Array Division
题目大意 给定你一个长为\(n\)的序列,问能否在最多一次取出某一元素然后插入到某一点后可以将整个序列分成两段使得其两段的元素之和相同. \(n \leq 10^5\) 题解 发现插入操作实际上是让某 ...
- 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 ...
- Array Division 808D
D. Array Division time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Array Division CodeForces - 808D (构造+实现)
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into t ...
- codeforces 808 D. Array Division(二分)
题目链接:http://codeforces.com/contest/808/problem/D 题意:有一串长度为n的数组,要求选择一个数字交换它的位置使得这串数能够分成两串连续的和一样的数组. 这 ...
- Educational Codeforces Round 21 D - Array Division (前缀和+二分)
传送门 题意 将n个数划分为两块,最多改变一个数的位置, 问能否使两块和相等 分析 因为我们最多只能移动一个数x,那么要么将该数往前移动,要么往后移动,一开始处理不需要移动的情况 那么遍历sum[i] ...
- Codeforces 797E - Array Queries
E. Array Queries 题目链接:http://codeforces.com/problemset/problem/797/E time limit per test 2 seconds m ...
- Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力
Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...
- CodeForces - 1175D Array Splitting(数组划分+后缀和+贪心)
You are given an array a1,a2,…,ana1,a2,…,an and an integer kk. You are asked to divide this array in ...
随机推荐
- Android安全测试(一)数字签名检测
1.测试环境 SDK: Java JDK, Android SDK. 下图为利用中间人攻击的手段,将智能电视上所有影片的封面图替换的实际效果图 利用中间人攻击还可以进行token获取等,所以不要轻易使 ...
- 剑指offer--day07
1.1 题目:反转链表:输入一个链表,反转链表后,输出新链表的表头. 1.2 思路:这道题,我们要做到的是反转链表,我们的思路是将前一个节点与后一个节点断开,然后让后一个节点指向前一个节点,这个过程就 ...
- JDK11 | 第四篇 : 增强API
文章首发于公众号<程序员果果> 地址 : https://mp.weixin.qq.com/s/O0xntC-JfeSBk-9x2QfwnA 一.简介 JDK 9~11 在语言语法方面有一 ...
- CSRF verification failed. Request aborted.错误解决办法
在Django项目的页面,提交form表单POST请求时,会出现报错:CSRF verification failed. Request aborted. 需要在form表单中加:{% csrf_to ...
- Lambda创建表达式目录树
一,如下代码: using System; using System.Linq.Expressions; namespace Demo { class Program { static void Ma ...
- C# 同步调用 异步调用 异步回调 多线程的作用
同步调用 : 委托的Invoke方法用来进行同步调用.同步调用也可以叫阻塞调用,它将阻塞当前线程,然后执行调用,调用完毕后再继续向下进行. 异步调用 :同步调用会阻塞线程,如果是要调用一项繁重的 ...
- markdown简介及基本语法
一.前言 Markdown是一种轻量级标记语言,它以纯文本形式(易读.易写.易更改)编写文档,并最终以HTML格式发布. Markdown也可以理解为将以MARKDOWN语法编写的语言转换成HTML内 ...
- React与Typescript整合
0. Typescript Typescript对于前端来说可以说是越来越重要了,前端的很多项目都用Typescript进行了重构.这主要得益于Typescript有比较好的类型支持,在编码的过程中可 ...
- openstack stein部署手册 10. 创建实例
# 建立网络(provider)与子网 openstack network create --share --external --provider-physical-network provider ...
- .net Console.ReadLine无效
代码中出现了 Console.ReadLine无效解决办法:把应用程序的输出类型改为 控制台应用程序