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 ...
随机推荐
- oracle--事物特性、锁、
update emp set comm = 100 where empno = 7369; 使用dba用户查看事务 ADDR XIDUSN XIDSLOT XIDSQN UBAFIL UBABLK U ...
- 快速排序 && 寻找第K大(小)的数
参考:https://minenet.me/2016/08/24/quickSort.html 快速排序 利用分治法可将快速排序的分为三步: 在数据集之中,选择一个元素作为"基准" ...
- HDU 1880 题解(字符串哈希)
题面: 魔咒词典 Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- (五:NIO系列) Reactor模式
出处:Reactor模式 本文目录 1. 为什么是Reactor模式 2. Reactor模式简介 3. 多线程IO的致命缺陷 4. 单线程Reactor模型 4.1. 什么是单线程Reactor呢? ...
- C# 委托和事件 实现窗体间的通信
例子 : 点击form1上的按钮打开form2窗口,在form2窗体中的文本框中输入一个值后,在点击form2窗体中按钮,在form2中的文本框中输入的值也会在form1中的文本框中出现. form1 ...
- 监听table滚动事件,滚动到底部时加载数据
mounted() { this.$refs.scrollTable.addEventListener( 'scroll',(event) => { this.getDistance(event ...
- wepy_two
2.代码高亮WebStorm/PhpStorm(其他工具参见:wepy官网代码高亮) (1)打开Settings,搜索Plugins,搜索Vue.js插件并安装. (2) 打开Settings,搜索F ...
- Docker镜像恢复与迁移
首先我们先删除掉 mycentos_new:1.1 镜像(注意先停止并删除所有引用了的容器) docker rmi mycentos_new:1.1 然后执行此命令进行恢复 mycentos_new: ...
- C# 开发 Windows 服务 使用Log4net 组件 不能生成日志文件
使用VS2012开发Windows服务,需要使用Log4net日志组件记录业务情况,但是始终生成不了日志文件. /// <summary> /// 入口方法 /// </summar ...
- Git命令——提交、查看、后退、前进
Git常用命令 1. 命令git init把这个目录变成Git可以管理的仓库: 2. 命令git commit把文件提交到仓库 这里需要注意的是,Git只能跟踪文本文件的改动,如txt文件,网页,所有 ...