D. Array Division
http://codeforces.com/contest/808/problem/D
一开始是没什么想法的,然后回顾下自己想题的思路,慢慢就想出来了。首先要找到是否有这样的一个位置使得:
前缀和 == 后缀和,可以二分来求。
然后可以这样想,如果对于每一个数字,我都去移动一下,每个位置都试一下,复杂度多少?显然不能承受。
然后优化下这个思路,有了一点思路,优化到极致,看看能不能过,不能过就换思路吧。一般来说,每一个位置都试一下,是很没必要的。一般都是有一个位置是最优的。
这个位置就是放在最前或者放在最后。可以这样去想。
如果原来的数组,是不存在这样的位置的,那么移动a[i]到某一个位置后,存在了这样的位置。那么肯定是把这个数字移动去了前缀和的贡献哪里(后缀和同理),因为不是移动到前缀和哪里,就相当于没移。
所以把它移动到第1位,前缀和就肯定包含它了。
最后还是被hack,细节写歪了
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = + ;
int n;
int a[maxn];
LL sum[maxn];
LL nowDel;
LL ask(int pos) {
if (pos < nowDel) {
return sum[pos] - a[pos] + a[nowDel];
} else return sum[pos];
}
LL ask2(int pos) {
if (pos >= nowDel) {
return sum[pos] - a[nowDel] + a[pos + ];
} else return sum[pos];
}
bool tofind(int which) {
int be = , en = n;
while (be <= en) {
int mid = (be + en) >> ;
LL lef;
if (which == ) lef = ask(mid - );
else lef = ask2(mid - );
LL rig = sum[n] - lef;
if (lef < rig) be = mid + ;
else en = mid - ;
}
LL lef;
if (which == ) lef = ask(en);
else lef = ask2(en);
return lef * == sum[n];
}
void work() {
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
sum[i] = sum[i - ] + a[i];
}
// nowDel = 2;
// tofind(2);
for (int i = ; i <= n; ++i) {
nowDel = i;
if (tofind()) {
printf("YES\n");
return;
}
if (tofind()) {
printf("YES\n");
return;
}
}
printf("NO\n");
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
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 ...
- 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 808D. Array Division
题目大意 给定你一个长为\(n\)的序列,问能否在最多一次取出某一元素然后插入到某一点后可以将整个序列分成两段使得其两段的元素之和相同. \(n \leq 10^5\) 题解 发现插入操作实际上是让某 ...
- Educational Codeforces Round 21 D - Array Division (前缀和+二分)
传送门 题意 将n个数划分为两块,最多改变一个数的位置, 问能否使两块和相等 分析 因为我们最多只能移动一个数x,那么要么将该数往前移动,要么往后移动,一开始处理不需要移动的情况 那么遍历sum[i] ...
- 【multimap的应用】D. Array Division
http://codeforces.com/contest/808/problem/D #include<iostream> #include<cstdio> #include ...
- codeforces 808 D. Array Division(二分)
题目链接:http://codeforces.com/contest/808/problem/D 题意:有一串长度为n的数组,要求选择一个数字交换它的位置使得这串数能够分成两串连续的和一样的数组. 这 ...
- Codeforces D. Array Division
题目链接:http://codeforces.com/contest/808/problem/D 题意: 这一题给你一个数组,你可以调换某一个数的位置,使得这个数组可以分成2半,前半段的和等于后半段( ...
- CF808D STL
D. Array Division time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
随机推荐
- codeforces 659D D. Bicycle Race(水题)
题目链接: D. Bicycle Race time limit per test 1 second memory limit per test 256 megabytes input standar ...
- MongoDB主从复制,主主复制
MongoDB主从复制,是不需要像mysql那样从数据库事先要完整的主数据快照背景介绍:mongodb支持一主一从或多从复制1) master节点: mongod --dbpath=/usr/M ...
- ambari快速安装hadoop
资源下载http://www.cnblogs.com/bfmq/p/6027202.html 大家都知道hadoop包含很多的组件,虽然很多都是下载后解压简单配置下就可以用的,但是还是耐不住我是一个懒 ...
- MFC ListBox 设置水平长度
在*.rc资源 设置可以水平滚动, 垂直滚动 但是 水平滚动无效,水平方向 一直无法显示 完整 设置代码如下 m_listBox.SetHorizontalExtent(2000); m_listBo ...
- c++常用函数STL
完c++快一年了,感觉很有遗憾,因为一直没有感觉到c++的强大之处,当时最大的感觉就是这个东西的输入输出比C语言要简单好写. 后来我发现了qt,opencv,opengl,原来,c++好玩的狠. 在这 ...
- android TextView selector点击样式改变
1.selector 从单词的意思来说:选择器,就是对你的目标的控制.selector主要是用在ListView的item单击样式和TextView和Button的点击样式. 2.主要属性介绍: an ...
- 怎么解决sublime的插件自动被禁用
前两天,我的sublime text安装的很多插件都被自动禁用了,每次都要我自己重新启用一下才可以,后来从网上找到了解决方法. 找到“设置”-“Package Settings”-“Package C ...
- CPU密集型和IO密集型
对于Python如果是CPU密集型应该用多进程模型(大量的计算) 如果是IO密集型应该用多线程模型(数据的读取写入.网络IO数据传输) 由于GIL的存在,CPython不能有效的利用多核处理器,表 ...
- Depth Buffer
Up until now there is only one type of output buffer you've made use of, the color buffer. This chap ...
- ZOJ 1586 QS Network Kruskal求最小生成树
QS Network Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the ...