[CodeForces 892A] Greed (Java中sort实现从大到小排序)

题目链接:http://codeforces.com/problemset/problem/892/A

具体的Java 中 sort实现降序排序:https://www.cnblogs.com/youpeng/p/10546797.html
Ac代码:

import java.util.Comparator;
import java.util.Scanner;

import static java.util.Arrays.sort;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            Integer[] arr = new Integer[n];
            long sum = 0, x;
            int max1, max2;
            for (int i = 0; i < n; i++) {
                x = scanner.nextInt();
                sum += x;
            }
            for (int i = 0; i < n; i++) {
                arr[i] = scanner.nextInt();
            }
            Comparator cmp = new Mycomparator();
            sort(arr, cmp);
            max1 = arr[0];
            max2 = arr[1];
            if (sum > max1 + max2) {
                System.out.println("NO");
            } else {
                System.out.println("YES");
            }
        }
    }

}

class Mycomparator implements Comparator<Integer> {
    @Override
    public int compare(Integer a, Integer b) {
        return a > b ? -1 : 1;
    }
}

猜你喜欢

转载自www.cnblogs.com/youpeng/p/10546754.html