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的更多相关文章

  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. Array Division 808D

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

  3. Array Division CodeForces - 808D (构造+实现)

    Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into t ...

  4. Codeforces 808D. Array Division

    题目大意 给定你一个长为\(n\)的序列,问能否在最多一次取出某一元素然后插入到某一点后可以将整个序列分成两段使得其两段的元素之和相同. \(n \leq 10^5\) 题解 发现插入操作实际上是让某 ...

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

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

  6. 【multimap的应用】D. Array Division

    http://codeforces.com/contest/808/problem/D #include<iostream> #include<cstdio> #include ...

  7. codeforces 808 D. Array Division(二分)

    题目链接:http://codeforces.com/contest/808/problem/D 题意:有一串长度为n的数组,要求选择一个数字交换它的位置使得这串数能够分成两串连续的和一样的数组. 这 ...

  8. Codeforces D. Array Division

    题目链接:http://codeforces.com/contest/808/problem/D 题意: 这一题给你一个数组,你可以调换某一个数的位置,使得这个数组可以分成2半,前半段的和等于后半段( ...

  9. CF808D STL

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

随机推荐

  1. 《java编程思想》:异常丢失

    finally子句的不恰当使用,会造成异常的丢失,此处列举两种典型的错误使用示例.编程中要避免这种情况 示例一: try{ throw new ExceptionA(); }finally{ thro ...

  2. C语言中的文件操作

    按照字符的方式读取文件 按照行的方式读取文件 按照数据块的方式读取文件 按照格式化的方式读取文件 文件分类 记录文件:具有一定的结构记录组成,分为定长和不定长两种方式 流式文件:按照一个字符一个字符( ...

  3. 【Lintcode】177.Convert Sorted Array to Binary Search Tree With Minimal Height

    题目: Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. ...

  4. SQL 优化总结(二) 索引

     索引 1.索引的建立 缺省情况下建立的索引是非群集索引,但有时它并不是最佳的:合理的索引设计要建立在对各种查询的分析和预测上. 一般来说: (1) 有大量重复值.且经常有范围查询(between, ...

  5. IHE-PIX 备注

    IHE给出了各个Actor之间如何通讯的建议: 1.       应用程序通讯时必须用MLLP包装或者解析. 2.       客户端建立连接后,服务器端必须用此连接进行应答.客户端可以继续用此连接启 ...

  6. 02_mysql卸载和安装

    如果只是随便地反安装/uninstall之后,在文件系统或者是注册表里面可能会残留一些东西,这些东西如果不及时清除掉,再装可能会出现问题,你新装的会用不了. #Path to installation ...

  7. [hdu4738]求桥模板

    oj问题,待修改,存档. #include<stdio.h> #include<iostream> #include<cstdio> #include<sta ...

  8. openstack官方指导书

    openstack官方网站:https://docs.openstack.org/ 由于openstack的官方文档有点多,所以这里对其进行梳理一下 Release Notes 发布版本 新功能,升级 ...

  9. 怎么将vim的剪切版设置成系统的剪切版

    如果你用vim敲完了代码,怎么把代码提交到ACMoj的粘贴版上呢. 这是个问题. 去网上查了一下,首先有人说可以在vimrc里面添加 set clipboard=unnamed 我试了一下,没有效果. ...

  10. EntityFramework数据库配置(code frist)

    什么也不说先贴代码 <?xml version="1.0" encoding="utf-8"?> <configuration> < ...