cf1478C Nezzar and Symmetric Array

Long time ago there was a symmetric array a1,a2,…,a2n consisting of 2n distinct integers. Array a1,a2,…,a2n is called symmetric if for each integer 1≤i≤2n, there exists an integer 1≤j≤2n such that ai=−aj.

For each integer 1≤i≤2n, Nezzar wrote down an integer di equal to the sum of absolute differences from ai to all integers in a, i. e. di=∑2nj=1|ai−aj|.

Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.

Input
The first line contains a single integer t (1≤t≤105) — the number of test cases.

The first line of each test case contains a single integer n (1≤n≤105).

The second line of each test case contains 2n integers d1,d2,…,d2n (0≤di≤1012).

It is guaranteed that the sum of n over all test cases does not exceed 105.

Output
For each test case, print “YES” in a single line if there exists a possible array a. Otherwise, print “NO”.

You can print letters in any case (upper or lower).

Example
inputCopy
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
outputCopy
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,−3,−1,3] is one possible symmetric array that generates the array d=[8,12,8,12].

In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d.
题意
有2n个整数,每个数各不相同,且对于其中的任意数总能找到他的相反数,给你每个数与其他所有数的绝对值之差的和,问你能不能还原出原来的数
思路
我们先从样例中给的两个YES的输入着手,会发现每个数在输入中都会出现两次,进一步的思考,我们可以很快的发现,由于|A-B|与|-A-(-B)|一定是相等的,对于任意d[i]存在与之相同的数。由此可以找出第一个规律:

输入中的每个数出现且仅出现2次

我们先从最简单的数据开始找规律
假如我们有4个数,分别为a,b,-a,-b,那么对于第一个数
在这里插入图片描述
我们假设a,b均为正数,
此时d有两种情况
在这里插入图片描述
并且d1>d2的
由此我们可以想到,我们完全可以将输入的d从大到小排序并除重,此时d1最大,即为2*n个第一个原数组:2n*a[1]

对于除a[1]外的任意数字(只看正数)都比其小,故的d[1]=n*2a[1]

对于d2,其应当为a[2]计算得到的:2a[1]+(n-1)*2a[2]

对于a[2],只有a[1]比他大,其他数字均比其小

由此我们就可以推出公式了
在用最后一个样例举一个例子
在这里插入图片描述
代码

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
const int inf=0x3f3f3f3f;
#define ll long long
#define ull unsigned long long
#define mm(w,v) memset(w,v,sizeof(w))
#define f(x,y,z) for(int x=(y),_=(z);x<_;++x)
const int modn=1e9+7;
const int MAXN=1000000;
using namespace std;
ll ans[100000+10];
ll d[100000+10];
map<ll,int> hs; 
bool cmp(ll a,ll b) {
	return a>b;
}
void solve() {
	int t;
	scanf("%d",&t);
	while(t--) {
		hs.clear();
		int n;
		scanf("%d",&n);
		int cnt=0;
		f(i,0,2*n) {
			ll ip;
			scanf("%lld",&ip);
			if(hs[ip]==0) {
				d[cnt++]=ip;
				hs[ip]=1;
			}
			else {
				hs[ip]++;
			}
		}
		sort(d,d+cnt,cmp);
		bool ok=true;
		ll pre=0;
		int preneed=0;
		for(int i=0;i<cnt;i++) {
			if(hs[d[i]]!=2) {
				ok=false;
				break;
			}
			ll need=n*2;
			if(preneed==0) {
				f(j,0,i) {
					d[i]-=2*ans[j];
					pre+=2*ans[j];
					preneed+=2;
					need-=2;
				}
			}
			else {
				d[i]-=(pre+2*ans[i-1]);
				pre+=2*ans[i-1];
				need-=(preneed+2);
				preneed+=2;
			}
			if(d[i]%need!=0||d[i]<=0) {
				ok=false;
				break;
			}
			ans[i]=d[i]/need;
			if(i>0&&ans[i]>=ans[i-1]) {
				ok=false;
				break;
			}
		}
		if(ok) {
			printf("YES\n");
		}
		else {
			printf("NO\n");
		}
	}
}
int main(void) {
	solve();
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ljw0925

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值