Codeforce A. Everything Everywhere All But One

    

You are given an array of nn integers a1,a2,…,ana1,a2,…,an. After you watched the amazing film "Everything Everywhere All At Once", you came up with the following operation.

In one operation, you choose n−1n−1 elements of the array and replace each of them with their arithmetic mean (which doesn't have to be an integer). For example, from the array [1,2,3,1]. we can get the array [2,2,2,1] if we choose the first three elements, or we can get the array[4/3,4/3,3,4/3], if we choose all elements except the third.

Is it possible to make all elements of the array equal by performing a finite number of such operations?

———————————————————————————————————————————

您将获得一个长度为n的数组整数,在一次操作中,您可以选择n−1n−1元素,并用它们的算术平均值(不必是整数)替换它们中的每一个元素。例如,从数组[1,2,3,1]我们可以得到数组[2,2,2,1],如果我们选择前三个元素;或者我们可以得到数组[4/3,4/3,3,4/3],如果我们选择除第三个元素之外的所有元素。

是否可以通过执行有限数量的此类操作来使数组的所有元素相等?

———————————————————————————————————————————

Input

The first line of the input contains a single integer tt (1≤t≤2001≤t≤200)  — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (3≤n≤503≤n≤50)  — the number of integers.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1000≤ai≤100).

Output

For each test case, if it is possible to make all elements equal after some number of operations, output YESYES. Otherwise, output NONO.

You can output YESYES and NONO in any case (for example, strings yEsyEs, yesyes, YesYes will be recognized as a positive response).

Example

input

4
3
42 42 42
5
1 2 3 4 5
4
4 3 2 1
3
24 2 22

output

YES
YES
NO
NO

_____________________________________________________________________________

       首先须知道本题若能使所有元素都相等,那么即是选择了数组中的一个元素,其他元素的算术平均值与它相等,这个算术平均值可以是分数。

        关键点:如果对数组中的所有数都选择一次后还不能”YES”,那么就说明这个数组一定只能打印“NO”了。解释如下:假如我们进行了一次操作,该数组不是所有元素均相等,所以我们要在进行过操作的数组上继续操作。假设数组有n-1个x,一个y,其中x!=y。若我们选择y的话,那么数组将不改变,所以我们选择一个x,得到的其他数的算术平均值是((n-2)*x+y)/n-1,因为x!=y,所以算数平均值和x一定不可能相等,也就是说如果第一次操作不符合“YES”,那么就不需要在它的基础上再进行操作了。

        所以我们只需要在输入数组元素的时候求得数组元素之和,然后每次选哪个数就用和减去这个数,得到剩下元素的元素和,判断那个数*(n-1)与它是否相等。至于为什么不把剩下元素的元素和除以(n-1)判断它与选取的元素是否相等,是因为除法可能会导致存在小数的问题。

 代码如下:

#include<iostream>
using namespace std;
int main(){
    int t,n,i;
    cin>>t;
    while(t--){
        cin>>n;
        int a[50],sum=0,s;
        for(i=0;i<n;i++){
            cin>>a[i];
            sum+=a[i];
        }
        //选择数组中的每一个数
        for(i=0;i<n;i++){
            s=sum-a[i];//其他元素的和
            //判断数组所有元素是否可以相等
            if(s==a[i]*(n-1)){
                cout<<"YES"<<endl;
                break;
            }
        }
        //判断有没有打印过YES,有没有判断到最后一个元素
        if(i==n){
            cout<<"NO"<<endl;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值