当前位置:网站首页>905. Interval selection
905. Interval selection
2022-08-05 03:24:00 【Hunter_Kevin】
905. 区间选点
给定 N 个闭区间 [ai,bi],请你在数轴上选择尽量少的点,使得每个区间内至少包含一个选出的点.
输出选择的点的最小数量.
位于区间端点上的点也算作区间内.
输入格式
第一行包含整数 N,表示区间数.
接下来 N 行,每行包含两个整数 ai,bi,表示一个区间的两个端点.
输出格式
输出一个整数,表示所需的点的最小数量.
数据范围
1≤N≤105,
−109≤ai≤bi≤109
输入样例:
3
-1 1
2 4
3 5
输出样例:
2
代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;
typedef pair<int,int> PII;
PII a[N];
bool comp(PII x, PII y)
{
return x.second < y.second;
}
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i++)cin >> a[i].first >> a[i].second;
// Sort by the size of the right endpoint of the interval
sort(a,a+n,comp);
// Each time the right endpoint of the interval is selected as the selection point
int r = a[0].second;
int res = 1;
for(int i = 1; i < n; i++){
if(a[i].first > r){
//如果当前区间的左端点>选点 That is, the interval does not include selected points,Then update the interval boundary and the new selection point
res++;
r = a[i].second;
}//如果当前区间的左端点<=选点,then ignore the current range,Continue to judge the next interval
}
cout << res << endl;
return 0;
}
边栏推荐
- You may use special comments to disable some warnings. 报错解决的三种方式
- leetcode-每日一题1403. 非递增顺序的最小子序列(贪心)
- 惨遭打脸:字节某部门竟有这么多测试员
- 用CH341A烧录外挂Flash (W25Q16JV)
- Syntax basics (variables, input and output, expressions and sequential statement completion)
- 引领数字医学高地,中山医院探索打造未来医院“新范式”
- 高项 02 信息系统项目管理基础
- 达梦8数据库导出导入
- MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
- 如何在WordPress中添加特定类别的小工具
猜你喜欢

Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals

The Tanabata copywriting you want has been sorted out for you!

腾讯云【Hiflow】新时代自动化工具

静态方法获取配置文件数据

你要的七夕文案,已为您整理好!

Getting Started with Kubernetes Networking

基于生长的棋盘格角点检测方法

Flink 1.15.1 Cluster Construction (StandaloneSession)

After the large pixel panorama is completed, what are the promotion methods?

21天学习挑战赛(2)图解设备树的使用
随机推荐
sql怎么找字段里所有数据为空的字段
burp安装及代理设置
Slapped in the face: there are so many testers in a certain department of byte
(十一)元类
presto启动成功后出现2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer
冒泡排序与快速排序
How to transfer a single node of Youxuan database to a cluster
2022高处安装、维护、拆除考试题模拟考试题库及在线模拟考试
运维监控系统之Open-Falcon
YYGH-13-客服中心
YYGH-13-Customer Service Center
STM32 uses stm32cubemx LL library series tutorial
【 genius_platform software platform development 】 : seventy-six vs the preprocessor definitions written cow force!!!!!!!!!!(in the other groups conding personnel told so cow force configuration to can
The usage of try...catch and finally in js
Likou - preorder traversal, inorder traversal, postorder traversal of binary tree
静态方法获取配置文件数据
Distributed systems revisited: there will never be a perfect consistency scheme...
QT language file production
Bubble Sort and Quick Sort
结构体初解