贪心算法经典例题:1,选择不相交区间 2,区间选点 3,区间覆盖

1,选择不相交区间:数轴上有n个开区间(ai,bi)。选择尽量多个区间,是的这些区间两两没有公共点

#include <stdio.h>
#include <stdlib.h>
#include<algorithm>
using namespace std;
//选择不想交区间
struct Node{
    int x, y;
}map[100+10];

int cmp(Node a, Node b){
    if(a.y != b.y) return a.y < b.y;
    return a.x < b.y;
}

int main(){
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d %d" , &map[i].x, &map[i].y);
    }
    sort(map,map+n,cmp);
    //开始判断,第一个区间是一定要选的
    int start = map[0].y;
    int count = 1;  //选了第一个,直接从第一个的后端点开始
    int n_last = n-1; //剩余区间数目
    
    while(n_last > 0){
        if(map[n-n_last].x >= start){
            count++;

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值