贪心求解区间选点问题(c++实现)

cpp 复制代码
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e5+10;
struct Range{
    int l,r;
}range[N];

bool cmp(struct Range a,struct Range b){
    return a.r<b.r;
}

int main(){
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        int l,r;
        cin>>l>>r;
        range[i]={l,r};
    }
    sort(range,range+n,cmp);
    
    int res=0,ed=-2e9;
    for(int i=0;i<n;i++){
        if(ed<range[i].l){
            res++;
            ed=range[i].r;
        }
    }
    cout<<res;
}
相关推荐
Larry_Yanan26 分钟前
Qt多进程(六)共享内存和信号量
开发语言·qt
BD_Marathon26 分钟前
bean基础配置
java·开发语言
东方忘忧29 分钟前
Qt使用QDesktopServices::openUrl打开系统默认应用(如浏览器,文件,文件夹和邮件)
开发语言·qt
计算机内卷的N天30 分钟前
qt的模态和非模态状态
开发语言·qt
半桶水专家8 小时前
go语言中的结构体嵌入详解
开发语言·后端·golang
长安er8 小时前
LeetCode215/347/295 堆相关理论与题目
java·数据结构·算法·leetcode·
元亓亓亓8 小时前
LeetCode热题100--62. 不同路径--中等
算法·leetcode·职场和发展
在屏幕前出油9 小时前
二、Python面向对象编程基础——理解self
开发语言·python
小白菜又菜9 小时前
Leetcode 1925. Count Square Sum Triples
算法·leetcode
粉红色回忆9 小时前
用链表实现了简单版本的malloc/free函数
数据结构·c++