[CF808D] Array Division
Description
给你一组数,问是否可以最多移动一个数,使得这一串数可以分成两个部分,每一部分所有数的和相等。
Solution
要么是一个前缀里抽调一个数,要么是一个前缀再加上剩下部分的一个数
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(n + 2);
for (int i = 1; i <= n; i++)
cin >> a[i];
multiset<int> s_left, s_right;
for (int i = 1; i <= n; i++)
s_right.insert(a[i]);
int sum = 0;
for (int i = 1; i <= n; i++)
sum += a[i];
if (sum & 1)
{
cout << "NO" << endl;
return 0;
}
int presum = 0;
for (int i = 1; i <= n; i++)
{
s_left.insert(a[i]);
s_right.erase(s_right.find(a[i]));
presum += a[i];
if (presum * 2 == sum)
{
cout << "YES" << endl;
return 0;
}
else if (presum * 2 < sum)
{
if (s_right.find(sum / 2 - presum) != s_right.end())
{
cout << "YES" << endl;
return 0;
}
}
else
{
if (s_left.find(-sum / 2 + presum) != s_left.end())
{
cout << "YES" << endl;
return 0;
}
}
}
cout << "NO" << endl;
}