贪心求解区间选点问题(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;
}
相关推荐
顾子茵4 分钟前
c++从入门到精通(四)--动态内存,模板与泛型编程
java·开发语言·c++
电信2301杨臣20 分钟前
QT---信号与槽
开发语言·qt
ai.Neo25 分钟前
牛客网NC22015:最大值和最小值
数据结构·c++·算法
明月看潮生26 分钟前
青少年编程与数学 02-019 Rust 编程基础 07课题、字符串
开发语言·青少年编程·rust·编程与数学
抽风的雨61027 分钟前
【python基础知识】Day26 函数
开发语言·python
编程有点难1 小时前
Python训练打卡Day22
开发语言·python·机器学习
Swift社区1 小时前
LeetCode 高频题实战:如何优雅地序列化和反序列化字符串数组?
算法·leetcode·职场和发展
醍醐三叶2 小时前
C++核心编程--3 函数提高
c++
徐子童2 小时前
《从零开始入门递归算法:搜索与回溯的核心思想 + 剑指Offer+leetcode高频面试题实战(含可视化图解)》
算法
咕噜咕噜啦啦2 小时前
Python爬虫入门
开发语言·爬虫·python