持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第26天,点击查看活动详情
本文已参与「新人创作礼」活动,一 起开启掘金创作之路
题目
Long time ago there was a symmetric array consisting of distinct integers. Array is called symmetric if for each integer , there exists an integer such that . For each integer , Nezzar wrote down an integer equal to the sum of absolute differences from to all integers in , i. e. . Now a million years has passed and Nezzar can barely remember the array and totally forget . Nezzar wonders if there exists any symmetric array consisting of distinct integers that generates the array .
中文翻译
很久以前,有一个对称数组a1,a2,...,a2n由2n个不同的整数组成。如果对于每个整数1≤i≤2n,存在一个整数1≤j≤2n,使ai=-aj,则数组a1,a2,...,a2n被称为对称的。 对于每个整数1≤i≤2n,尼扎尔写下了一个整数di,等于从ai到a中所有整数的绝对差之和,即di=∑2nj=1|ai-aj|。 现在一百万年过去了,Nezzar几乎记不起这个数组d,而完全忘记了a。Nezzar想知道是否存在任何由2n个不同的整数组成的对称数组a,可以生成数组d。
解法
对于这题我们很容易发现di一定是成对出现的所以我们第一轮筛去不是成对出现的然后我们写出这题的数学公式进行推导所以我们首先去想如何拆绝对值然后我们发现有两种可能性,一种是那么我们就可以直接去除绝对值另一种改变符号去除绝对值然后我们就能发现对于如果n为4 这样我们就很容易看出规律来了
Code
int a[N];
void solve()
{
int n; cin >> n;
n += n;
map<int,int> mp;
for(int i = 1; i <= n; i++) cin >> a[i],mp[a[i]] ++;
sort(a + 1,a + 1 + n);
int m = unique(a + 1,a + 1 + n) - a - 1;
bool f = true;
if(m + m != n) f = false;
for(auto x : mp) if(x.se & 1) f = false;
rep(i,m) {
if(a[i] & 1) f= false;
else a[i] /= 2;
}
if(!f) {
NO return;
}
int cnt = 0;
for(int i = m ; i >= 1; i--) {
if((a[i] - cnt) % i) {
NO return;
}else {
int k = (a[i] - cnt) / i;
if(k < 1) {
NO return;
}else cnt += k;
}
}
YES
}