优先级队列(最大堆),X从大到小遍历每一个点,y应该是逐渐递增的,当y减小时,这个点不满足。
但是只能过60%数据,复杂度过大。优化:按照y降序,x升序,过了70%或者80%;不会优化了
import java.util.PriorityQueue; import java.util.Scanner; public class Main { static class Node { public int x; public int y; public Node(int x, int y){ this.x = x; this.y = y; } } public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); PriorityQueue<Node> priorityQueue = new PriorityQueue<>((o1, o2) -> { if(o1.y == o2.y){ return Integer.compare(o1.x, o2.x); }else{ return Integer.compare(o2.y, o1.y); } }); for(int i = 0; i < n; i++){ priorityQueue.add(new Node(scanner.nextInt(), scanner.nextInt())); } int x = 0; while (!priorityQueue.isEmpty()) { Node temp = priorityQueue.poll(); if(temp.x >= x){ x = temp.x; System.out.println(temp.x + " " + temp.y); } } } }