zoukankan      html  css  js  c++  java
  • codeforce 1478C C. Nezzar and Symmetric Array 模拟 认真写 C

    codeforce 1478C C. Nezzar and Symmetric Array 模拟 认真写 C

    https://codeforces.com/contest/1478/problem/C

    C. Nezzar and Symmetric Array
    time limit per test
    2 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

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

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

    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.

    Input

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

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

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

    It is guaranteed that the sum of nn over all test cases does not exceed 105105.

    Output

    For each test case, print "YES" in a single line if there exists a possible array aa. Otherwise, print "NO".

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

    Example
    input
    Copy
    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
    
    output
    Copy
    YES
    NO
    NO
    NO
    NO
    YES
    
    Note

    In the first test case, a=[1,3,1,3]a=[1,−3,−1,3] is one possible symmetric array that generates the array d=[8,12,8,12]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 dd.

     分析

    按照题目说的

    很容易发现

    顺序没有用,对应的数一定有对应的结果

    写的时候一定认真写

    比赛的时候写的乱七八糟的,结果用了很久才写出来

    写错了好多好多次

    按照从小到大排序a数组

    那么d数组最后的面的就是n*2*a_0

    往前依次有规律递减两个数,d数组倒数第二个是(n-1)*2*a_1*2*a_0

    照样子写出来就可以了

    代码

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <math.h>
    #include <string.h>
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <iostream>
    #include <time.h>
    #include <queue>
    #include <list>
    #include <map>
    #include <set>
    #include <vector>
    #include <stack>
    #include <string.h>
    #include <bitset>
    #define sf scanf
    #define pf printf
    #define lf double
    #define p123 printf("123
    ");
    #define pn printf("
    ");
    #define pk printf(" ");
    #define p(n) printf("%d",n);
    #define pln(n) printf("%d
    ",n);
    #define s(n) scanf("%d",&n);
    #define ss(n) scanf("%s",n);
    #define ps(n) printf("%s",n);
    #define sld(n) scanf("%lld",&n);
    #define pld(n) printf("%lld",n);
    #define slf(n) scanf("%lf",&n);
    #define plf(n) printf("%lf",n);
    #define sc(n) scanf("%c",&n);
    #define pc(n) printf("%c",n);
    #define gc getchar();
    #define ll long long
    #define re(n,a) memset(n,a,sizeof(n));
    #define len(a) strlen(a)
    #define eps 1e-13
    #define zero(x) (((x) > 0? (x):(-x)) < eps)
    using namespace std;
    vector<ll > a(100005);
    vector<ll> b(100005);
    map<ll,ll> m;
    int main(){
        ll t;
        sld(t)
        while(t --){
            b.clear();
            a.clear();
            ll n,d;
            sld(n)
            ll x = 0ll;
            n*=2ll;
            ll flag = 0ll;
            ll tt;
            for(ll i = 0ll; i < n; i ++){
                sld(tt)
                if(m[tt]%2ll == 0){
                    a.push_back(tt);
                }
                m[tt] ++;
                if(tt % 2ll == 1ll){
                    flag = 1ll;
                }
                x ^= tt;
            }
            if(x != 0ll || flag == 1ll){
                puts("NO");
                continue;
            }
            sort(a.begin(),a.end());
            ll sum = 0ll;
            ll maxi = n;
            for(ll i = n/2ll-1ll; i >= 0ll ; i --){
                if( (a[i]-sum)%((i+1ll)*2ll) != 0ll){
                    flag = 1ll;
                    break;
                }
                if(a[i]-sum <= 0){
                    flag = 1ll;
                    break;
                }
                maxi = (a[i]-sum)/((i+1ll)*2ll);
                b.push_back(maxi);
                sum += maxi*2ll;
            }
            if(b[0ll] == 0ll){
                flag = 1ll;
            }
            for(ll i = 1ll; i < b.size(); i ++){
                //pld(b[i]) pk
                if(b[i] == b[i-1ll]){
                    flag = 1ll;
                    break;
                }
                if(b[i] == 0ll){
                    flag = 1ll;
                    break;
                }
            }
            if(flag == 1ll){
                puts("NO");
            }else{
                puts("YES");
            }
        }
        return 0;
    }

    https://codeforces.com/contest/1478/submission/105764657

    作者:kidgzz
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    POJ 1611 The Suspects 并查集
    POJ A Simple Problem with Integers 线段树的成段更新
    POJ 2367 Genealogical tree 拓扑排序
    《大道至简》读后感
    周总结报告
    周学习进度总结
    周总结报告
    周总结报告
    周总结报告
    教室派评价
  • 原文地址:https://www.cnblogs.com/Kidgzz/p/14347314.html
Copyright © 2011-2022 走看看