简单的水题

2018-08-09  本文已影响0人  portability

网易牛分苹果

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] cow_apples = new int[n];
        int i = 0, sum = 0, tmp = 0;
        while (i < n){
            tmp = scanner.nextInt();
            cow_apples[i++] = tmp;
            sum += tmp;
        }
        if (sum % n != 0){
            System.out.println(-1);
            return;
        }
        int target = sum / n;
        Arrays.sort(cow_apples);
        int count = 0;
        for (int j:
                cow_apples){
            if (((j - target) & 1) == 1){
                System.out.println(-1);
                return;
            }
            if (j < target){
                count += ((target -  j) >> 1);
            }
        }
        System.out.println(count);
    }
}
上一篇下一篇

猜你喜欢

热点阅读