输入第一行为一个整数k,代表有k个序列(k<100),接下来有2*k行:偶数行为一个整数n,代表给定序列的长度(1<=n<=100,000)奇数行包含n个元素,a1,a2...an,代表序列中的元素(0<=ai<=100,000)
输出k行,每行一个YES或者NO
1 5 1 3 3 2 1
YES
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n=in.nextInt(); while(n>0){ n--; int m=in.nextInt(); Set<Integer> set=new HashSet(); while(m>0){ set.add(in.nextInt()); m--; } if(set.size()>3){ System.out.println("NO"); continue; }else if(set.size()<=2){ System.out.println("YES"); }else{ int[] arr=new int[3]; int idx=0; for(int x:set){ arr[idx++]=x; } Arrays.sort(arr); if(arr[0]+arr[2]==arr[1]*2){ System.out.println("YES"); }else{ System.out.println("NO"); } } } } }
import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int k = Integer.parseInt(br.readLine()); for(int i = 0;i < k;i++){ int n = Integer.parseInt(br.readLine()); String[] str = br.readLine().split(" "); int[] arr = new int[n]; for(int j = 0;j < n;j++){ arr[j] = Integer.parseInt(str[j]); } System.out.println(eql(arr)); } } public static String eql(int[] arr){ Arrays.sort(arr); int a = arr[0]; int b = arr[arr.length-1]; if(arr.length == 2){ return "YES"; } if((a+b) % 2 != 0){ return "NO"; } int mid = (a+b)/2; int offset = b - mid; for(int i = 0;i < arr.length;i++){ if(!(arr[i] + offset == mid || arr[i] - offset == mid || arr[i] == mid)){ return "NO"; } } return "YES"; } }很黄很暴力