HRZ的序列

时间限制1s,空间64MB

问题描述

相较于咕咕东,瑞神是个起早贪黑的好孩子,今天早上瑞神起得很早,刷B站时看到了一个序列a,他对这个序列产生了浓厚的兴趣。
他好奇是否存在一个数K,使得一些数加上K,一些数减去K,一些数不变,使得整个序列中所有的数相等。
其中对于序列中的每个位置上的数字,至多只能执行一次加运算或减运算或是对该位置不进行任何操作。
由于瑞神只会刷B站,所以他把这个问题交给了你!

Input

输入第一行是一个正整数t表示数据组数。
接下来对于每组数据,输入的第一个正整数n表示序列a的长度,随后一行有n个整数,表示序列a。

Output

输出共包含t行,每组数据输出一行。对于每组数据,如果存在这样的K,输出"YES",否则输出“NO”。(输出不包含引号)

Sample input

2
5
1 2 3 4 5
5
1 2 3 4 5

Sample output

NO
NO

数据范围

在这里插入图片描述

解题思路

数列有一下几种情况:

  1. 只有一种数,输出"YES"
  2. 只有两种数,输出"YES"
  3. 只有三种数,如果满足其中两个相加等于第三个乘2,输出"YES",否则"NO"
  4. 三种以上的数,输出"NO"

使用pair记录一种数是否出现过以及这个数是多少,具体看代码即可。

完整代码

//#pragma GCC optimize(2)//比赛禁止使用!
//#pragma G++ optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

const int maxn=100000+10;
int t,n;
long long a[maxn];
pair<long long,bool> p1,p2,p3;
int getint()
{
    int x=0,s=1;
    char ch=' ';
    while(ch<'0' || ch>'9')
    {
        ch=getchar();
        if(ch=='-') s=-1;
    }
    while(ch>='0' && ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*s;
}
long long getlong_long()
{
    long long x=0,s=1;
    char ch=' ';
    while(ch<'0' || ch>'9')
    {
        ch=getchar();
        if(ch=='-') s=-1;
    }
    while(ch>='0' && ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*s;
}
int main()
{
    t=getint();
    while(t--){
        n=getint();
        bool flag=true;
        p1.second=false; p2.second=false; p3.second=false;
        for (int i=1; i<=n; i++)
        {
            long long temp=getlong_long();
            if(!p1.second){ p1.first=temp; p1.second=true; }//是第一种数,并且第一次出现
            else if(p1.second && p1.first==temp){ continue; }//是第一种数,出现过
            else if(!p2.second){ p2.first=temp; p2.second=true; }//是第二种数,并且是第一次出现
            else if(p2.second && p2.first==temp){ continue; }//是第二种数,出现过
            else if(!p3.second){ p3.first=temp; p3.second=true;}//是第三种数,并且第一次出现
            else if(p3.second && p3.first==temp){ continue; }//是第三种数,出现过
            else { flag=false;}//三种数都不是,超过了三种数
        }
        if(!flag) cout<<"NO"<<endl;//情况4
        else if(!p2.second || !p3.second || p1.first+p2.first==p3.first*2 || p1.first+p3.first==p2.first*2 || p2.first+p3.first==p1.first*2)
            cout<<"YES"<<endl;//情况1,2,3的部分
        else cout<<"NO"<<endl;//情况3另一部分
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值