AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 校园
  • 应用
  • 文章
    • 题解
    • 分享
    • 问答
  • 吐槽
  • 登录/注册

AcWing 422. 校门外的树 0.04 AC币

作者: 作者的头像   Acwer ,  2022-06-23 18:27:00 ,  所有人可见 ,  阅读 1


0


import java.util.*;

public class Main {
    public static void main(String[] args){
        List<PII> list = new ArrayList();
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for(int i=0;i<n;i++) {
            int l = scanner.nextInt();
            int r = scanner.nextInt();
            list.add(new PII(l,r));
        }
        //左端点进行排序
        Collections.sort(list, new Comparator<PII>() {
            @Override
            public int compare(PII o1, PII o2) {
                return o1.first-o2.first;
            }
        });

        int count = 0;   //记录有几个区间
        int st = (int) -2e9;
        int ed = (int)-2e9;

        for(PII p:list){
            if(p.first>ed){
                count++;
                st = p.first;
                ed = p.second;
            }
            else{
                ed = Math.max(ed,p.second);
            }
        }
        System.out.println(count);

    }
}

class PII {
    int first;
    int second;
    PII(int first,int second){
        this.first=first;
        this.second=second;
    }
}

0 评论

你确定删除吗?

© 2018-2022 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息