Codeforces Round #371 (Div. 2) B

本文介绍了一种算法,用于判断一组数字通过加减相同数值的操作能否使所有数字相等。首先对数字进行排序,然后统计不同数字的数量。根据不同数量的情况给出是否可行的答案。对于三种不同数字且构成等差数列的情况,提供了实现方法。

传送门

题意:给你串数字 任意你取一个x 对某些数进行加x 某些数进行减x操作 是否能使得数组内的数全部相等

ps:某些数里的某些可能为0

思路:先排序 然后确定数组内有多少不同的数 大于3一定不能 小于2一定能 坑点在等于3 当数组里的数成等差数列时可以 否则不可以 例如:1 4 7 x=3; 1+3 = 4; 7-3 = 4; 1 3 4 就不可以了

AC代码:

 1 #include "iostream"
 2 #include "string.h"
 3 #include "stack"
 4 #include "queue"
 5 #include "map"
 6 #include "algorithm"
 7 #include "stdio.h"
 8 #include "math.h"
 9 #define ll long long
10 #define mem(a) memset(a,0,sizeof(a))
11 #define max(a,b) a > b ? a : b
12 #define min(a,b) a < b ? a : b
13 
14 using namespace std;
15 
16 int main()
17 {
18     int n,i,flag=0,a[100005],b[5];
19     cin>>n;
20     for(i=0; i<n; i++)
21         cin>>a[i];
22     sort(a,a+n);
23     b[0] = a[0];
24     int k = 1;
25     for(i=0; i<n-1; i++)
26     {
27         if(a[i+1]!=a[i])
28             b[k++] = a[i+1];
29     }
30     if(k > 3)
31         printf("NO\n");
32     else if(k==1 || k==2)
33         printf("YES\n");
34     else
35     {
36         if(b[1]-b[0] == b[2]-b[1])
37             printf("YES\n");
38         else
39             printf("NO\n");
40     }
41    return 0;
42 }

 

转载于:https://www.cnblogs.com/max88888888/p/5871268.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值