贪心算法(算法竞赛、蓝桥杯)--线段覆盖

1、B站视频链接:A29 贪心算法 P1803 线段覆盖_哔哩哔哩_bilibili

题目链接:凌乱的yyy / 线段覆盖 - 洛谷

00b7bedeac1049de92426d9969c26503.png

#include <bits/stdc++.h> 
using namespace std;

struct line{
	int l,r;
	bool operator<(line &b){
		return r<b.r;//重载小于号,按右端点排序 
	}
}L[1000005];
int n,last,cnt;

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&L[i].l,&L[i].r);
	}
	sort(L+1,L+n+1);//排序,右端点越小,总的个数就越多 
	for(int i=1;i<=n;i++){
		if(last<=L[i].l){//上一个的右端点小于当前的左端点 
			last=L[i].r;//成立则选择当前段并变成last 
			cnt++;
		}
	} 
	printf("%d\n",cnt);
	return 0;
}

 

 

相关推荐

  1. 算法基础(35)贪心算法详解

    2024-03-15 11:20:03       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-15 11:20:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-15 11:20:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-15 11:20:03       82 阅读
  4. Python语言-面向对象

    2024-03-15 11:20:03       91 阅读

热门阅读

  1. Python进阶学习(5)反射

    2024-03-15 11:20:03       40 阅读
  2. 举例说明计算机视觉(CV)技术的优势和挑战

    2024-03-15 11:20:03       43 阅读
  3. 三维的旋转平移矩阵形式

    2024-03-15 11:20:03       44 阅读
  4. openGauss 脚本源码浅析(1)—— simpleInstall

    2024-03-15 11:20:03       31 阅读
  5. SpringCloud Stream笔记整理

    2024-03-15 11:20:03       42 阅读
  6. 智障版本GPT3实现

    2024-03-15 11:20:03       44 阅读
  7. 什么是单向数据流

    2024-03-15 11:20:03       37 阅读
  8. 《软件工程》复试问答题总结

    2024-03-15 11:20:03       41 阅读