[CF1478C] Nezzar and Symmetric Array - 构造

Description

给出一个长度为 \(2n\) 的数组,问是否存在一个满足条件的 \(a\) 数组,其中的元素按相反数成对出现,并且满足给出的 \(d\) 数组(di 表示 ai 与 a 中每个元素之差的绝对值之和)。(\(1≤n≤10^5,0≤d_i≤10^{12}\)

Solution

考虑从外向内逐层构造

对于内层来说,外面每一层 j 的贡献都是 2aj,减掉这一部分后,剩下的又相当于一个新的外层,利用 an=dn/n 计算出 an

#include <bits/stdc++.h>
using namespace std;

#define int long long

void solve()
{
    int n;
    cin >> n;

    vector<int> d(n * 2);
    for (int i = 0; i < n * 2; i++)
        cin >> d[i];

    sort(d.begin(), d.end());

    vector<int> e;
    e.push_back(0);
    for (int i = n * 2 - 2; i >= 0; i -= 2)
    {
        if (d[i] == d[i + 1])
        {
            e.push_back(d[i]);
        }
        else
        {
            cout << "NO" << endl;
            return;
        }
    }

    d = e;

    vector<int> a(n + 2);
    int s = 0;
    for (int i = 1; i <= n; i++)
    {
        if (d[i] - s <= 0 || (d[i] - s) % (2 * (n - i + 1)))
        {
            cout << "NO" << endl;
            return;
        }
        a[i] = (d[i] - s) / (2 * (n - i + 1));
        if (a[i] == a[i - 1])
        {
            cout << "NO" << endl;
            return;
        }
        s += 2 * a[i];
    }
    cout << "YES" << endl;
}

signed main()
{
    ios::sync_with_stdio(false);

    int t;
    cin >> t;

    while (t--)
    {
        solve();
    }
}

相关文章:

  • 2021-12-25
  • 2022-12-23
  • 2021-07-25
  • 2022-12-23
  • 2021-06-02
  • 2022-01-25
  • 2022-12-23
  • 2021-11-17
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-23
  • 2021-10-21
  • 2022-12-23
  • 2021-02-15
  • 2021-11-14
相关资源
相似解决方案