题解 | #线段重合#

线段重合

https://www.nowcoder.com/practice/1ae8d0b6bb4e4bcdbf64ec491f63fc37


import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

public class Main {

    public static int n;

    public static class Pair {
        int l, r;
        public Pair(int l, int r) {
            this.l = l;
            this.r = r;
        }
    }

    public static Pair[] pairs;

    public static class sc {

        static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));

        public static int nextInt() throws IOException {
            st.nextToken();
            return (int) st.nval;
        }

        public static <T> void write(T o) {
            pw.print(o);
        }


    }

    public static void main(String[] args) throws IOException {
        n = sc.nextInt();
        pairs = new Pair[n];
        for (int i = 0; i < n; i++) {
            pairs[i] = new Pair(sc.nextInt(), sc.nextInt());
        }

        Arrays.sort(pairs, Comparator.comparing(a -> a.l));
        PriorityQueue<Integer> heap = new PriorityQueue<>(Comparator.comparingInt(a -> a));
        int ans = 0;
        for (int i = 0; i < n; i++) {
            while (!heap.isEmpty() && pairs[i].l >= heap.peek()) {
                heap.poll();
            }
            heap.add(pairs[i].r);
            ans = Math.max(ans, heap.size());
        }
        sc.pw.write(ans + "\n");
        sc.pw.flush();
    }

}

全部评论

相关推荐

我也曾抱有希望:说的好直白
点赞 评论 收藏
分享
10-30 23:23
已编辑
中山大学 Web前端
去B座二楼砸水泥地:这无论是个人素质还是专业素质都👇拉满了吧
点赞 评论 收藏
分享
2 收藏 评论
分享
牛客网
牛客企业服务