贪心求解区间选点问题(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;
}
相关推荐
YxVoyager20 小时前
Qt C++ :XML文件处理工具 <QXml>模块
xml·c++·qt
無斜20 小时前
【LabVIEW实用开发】--- LabVIEW调用python脚本
开发语言·python·labview
一只鱼^_21 小时前
力扣第470场周赛
数据结构·c++·算法·leetcode·深度优先·动态规划·启发式算法
CUMT_DJ1 天前
matlab计算算法的运行时间
开发语言·算法·matlab
greentea_20131 天前
Codeforces Round 65 A. Way Too Long Words(71)
c++
Overboom1 天前
[C++] --- 常用设计模式
开发语言·c++·设计模式
Univin1 天前
C++(10.4)
开发语言·数据结构·c++
YxVoyager1 天前
Qt C++ :QLayout 布局管理
c++·qt
KyollBM1 天前
每日羊题 (质数筛 + 数学 | 构造 + 位运算)
开发语言·c++·算法
Paul_09201 天前
golang面经——map模块和sync.Map模块
开发语言