【Codeforces】808D Array Division(前后两部分和相等)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CSDN___CSDN/article/details/87706039

http://codeforces.com/contest/808/problem/D

给你一个数组,问:是否可以通过移动一个数字的位置,求只能移动一次,使得这个数组前后部分的和相等,前后部分不一定等长

一个a数组储存数据,另一个b数组b[i]表示前i项和

如过存在这样的数,可以让前后的和相等,那么在没有移动之前,前后部分的差的绝对值等于这个数的两倍,所以前后部分差的绝对值一定是一个偶数

加上map映射

#include <iostream>
#include <cstring>
#include <map>
using namespace std;

typedef long long ll;

ll a[100000+5];
ll b[100000+5];
map<ll,ll> m1,m2;

int main()
{
	ll n;
	cin >> n;
	ll i;
	b[0] = 0;
	for(i=1;i<=n;i++)
	{
		cin >> a[i];
		m2[a[i]]++;//把所有的数都先放到m2中 
		b[i] = b[i-1] + a[i]; 
	}
	if(b[n] & 1)
	{
		cout << "NO";
		return 0;
	}
	for(i=1;i<=n;i++)
	{
		m1[a[i]]++;
		m2[a[i]]--;//从m2中取出,放入m1 
		ll s1 = b[i];
		ll s2 = b[n]-b[i];
		if(s1==s2)
		{
			cout << "YES";
			return 0;
		}
		if(s2-s1>0 && (s2-s1)%2==0 && m2[(s2-s1)/2]>0)//相差得那个数一定是在和较大的那一个部分中 
		{
			cout << "YES";
			return 0;
		}
		if(s1-s2>0 && (s1-s2)%2==0 && m1[(s1-s2)/2]>0)
		{
			cout << "YES";
			return 0;
		}
	}
	cout << "NO";
	return 0;
}

猜你喜欢

转载自blog.csdn.net/CSDN___CSDN/article/details/87706039