Codeforces 808D Array Division【思维】
来源:互联网 发布:淘宝购物如何追加评论 编辑:程序博客网 时间:2024/05/19 16:32
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position).
Inserting an element in the same position he was erased from is also considered moving.
Can Vasya divide the array after choosing the right element to move and its new position?
The first line contains single integer n (1 ≤ n ≤ 100000) — the size of the array.
The second line contains n integers a1, a2...an (1 ≤ ai ≤ 109) — the elements of the array.
Print YES if Vasya can divide the array after moving one element. Otherwise printNO.
31 3 2
YES
51 2 3 4 5
NO
52 2 3 4 5
YES
In the first example Vasya can move the second element to the end of the array.
In the second example no move can make the division possible.
In the third example Vasya can move the fourth element by one position to the left.
题目大意:
给你一个长度为N的序列,让你将其分成连续的两部分(不能为空),使得两部分的和都相等。
我们在分序列之前,可以选择不移动或者移动一个数到任意一个位子。
思路:
很套路,但是很简单。
我们首先维护出一个前缀和。
然后O(n)枚举分界线。
那么前一部分的和为Sumpre,后一部分的和为Sumback.
一共有三种情况:
①Sumpre==Sumback.那么结果就是YES.
②Sumpre>Sumback.那么我们考虑将前一部分的序列中,调出一个值为(Sumpre-Sumback)/2的值即可。这里map维护一下
③Sumpre<Sumback.那么我们考虑将后一部分的序列中,调出一个值为(Sumback-Sumpre)/2的值即可。这里map维护一下
注意序列不能为空。
注意奇偶。
以及某个值A【i】==sum【n】/2的情况也是YES.
Ac代码:
#include<stdio.h>#include<string.h>#include<map>using namespace std;#define ll __int64ll a[1000050];ll sum[1000050];int main(){ int n; while(~scanf("%d",&n)) { int flag=0; map<ll,int >pre; map<ll,int >back; memset(sum,0,sizeof(sum)); for(int i=1;i<=n;i++)scanf("%I64d",&a[i]); for(int i=1;i<=n;i++)sum[i]=sum[i-1]+a[i]; for(int i=1;i<=n;i++)back[a[i]]++; for(int i=1;i<=n;i++)if(sum[n]%2==0&&a[i]==sum[n]/2)flag=1; for(int i=1;i<n;i++) { back[a[i]]--; pre[a[i]]++; ll Sumpre=sum[i]; ll Sumback=sum[n]-sum[i]; if(Sumpre==Sumback)flag=1; else { if(Sumpre>Sumback) { if(i==1)continue; if((Sumpre-Sumback)%2==1)continue; ll x=(Sumpre-Sumback)/2; if(pre[x]>0)flag=1; } else { if(i==n-1)continue; if((Sumback-Sumpre)%2==1)continue; ll x=(Sumback-Sumpre)/2; if(back[x]>0)flag=1; } } } if(flag==1)printf("YES\n"); else printf("NO\n"); }}
- Codeforces 808D Array Division【思维】
- codeforces 808D Array Division(二分+思维)
- cf 808D Array Division(二分思维)@
- Codeforces 808D Array Division 题解
- 【Codeforces 808 D. Array Division】+ 二分
- 808D Array Division
- Educational Codeforces Round 21 D. Array Division
- Educational Codeforces Round 21 D Array Division
- codeforces Educational Codeforces Round 21 D. Array Division
- Educational Codeforces Round 21 D. Array Division(前缀和,二分)
- CF 808D D. Array Division(二分)
- codeforcodeforces 808D——Array Division(查找,set)
- CodeForces 721D. Maxim and Array(贪心,思维题)
- Codeforces 817D Imbalanced Array【思维】好题!
- Codeforces 483D Interesting Array【思维+线段树】
- Codeforces 149C Division into Teams 【思维】
- codeforces 765D 思维
- codeForces 612D(思维)
- android:在照片上绘制涂鸦
- Combinations
- unix域socket通信实例
- Spring Boot 是什么,有什么用。
- 搜索 B题
- Codeforces 808D Array Division【思维】
- Visual Studio 2015VS2015安装没有C++WIN32控制
- E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
- jQuery:jQuery UI API
- 恢复误删除的数据
- 简单介绍一下OGraph
- git的第一次使用
- Git的安装和使用
- 堆排序和TopK问题