Codeforces Round #698 (Div. 2)C. Nezzar and Symmetric Array(数学+思维)

114 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第26天,点击查看活动详情
本文已参与「新人创作礼」活动,一 起开启掘金创作之路

题目

Long time ago there was a symmetric array a1,a2,,a2na_1,a_2,\ldots,a_{2n} consisting of 2n2n distinct integers. Array a1,a2,,a2na_1,a_2,\ldots,a_{2n} is called symmetric if for each integer 1i2n1 \le i \le 2n, there exists an integer 1j2n1 \le j \le 2n such that ai=aja_i = -a_j. For each integer 1i2n1 \le i \le 2n, Nezzar wrote down an integer did_i equal to the sum of absolute differences from aia_i to all integers in aa, i. e. di=j=12naiajd_i = \sum_{j = 1}^{2n} {|a_i - a_j|}. Now a million years has passed and Nezzar can barely remember the array dd and totally forget aa. Nezzar wonders if there exists any symmetric array aa consisting of 2n2n distinct integers that generates the array dd.

中文翻译

很久以前,有一个对称数组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一定是成对出现的所以我们第一轮筛去不是成对出现的然后我们写出这题的数学公式进行推导di=jnaiajd_i = \sum_j^n|a_i-a_j|所以我们首先去想如何拆绝对值然后我们发现有两种可能性,一种是ajaia_j\le a_i那么我们就可以直接去除绝对值另一种改变符号去除绝对值然后我们就能发现对于如果n为4 d1=[a1+a2+a3+a4]d_1=[a_1+a_2+a_3+a_4] d2=[a2+a2+a3+a4]d_2=[a_2+a_2+a_3+a_4] d3=[a3+a3+a3+a4]d_3=[a_3+a_3+a_3+a_4] d4=[a4+a4+a4+a4]d_4=[a_4+a_4+a_4+a_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
}