贪心求解区间选点问题(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;
}
相关推荐
Coding小公仔2 小时前
C++ bitset 模板类
开发语言·c++
凌肖战2 小时前
力扣网C语言编程题:在数组中查找目标值位置之二分查找法
c语言·算法·leetcode
菜鸟看点2 小时前
自定义Cereal XML输出容器节点
c++·qt
小赖同学啊2 小时前
物联网数据安全区块链服务
开发语言·python·区块链
shimly1234562 小时前
bash 脚本比较 100 个程序运行时间,精确到毫秒,脚本
开发语言·chrome·bash
weixin_478689762 小时前
十大排序算法汇总
java·算法·排序算法
IT_10242 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
luofeiju3 小时前
使用LU分解求解线性方程组
线性代数·算法
new_zhou3 小时前
Windows qt打包编译好的程序
开发语言·windows·qt·打包程序