当前位置:网站首页>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;
}
边栏推荐
- ffmpeg 像素格式基础知识
- Call Alibaba Cloud oss and sms services
- word分栏小记
- ASP.NET应用程序--Hello World
- 链表的简单描述及代码的简单实现
- [Storage] Dawning Storage DS800-G35 ISCSI maps each LUN to the server
- sql server 安装提示用户名不存在
- How to find all fields with empty data in sql
- 2022高处安装、维护、拆除考试题模拟考试题库及在线模拟考试
- Open-Falcon of operation and maintenance monitoring system
猜你喜欢

Matlab drawing 3

Beyond YOLO5-Face | YOLO-FaceV2 officially open source Trick+ academic point full

Intersection of Boolean Operations in SuperMap iDesktop.Net - Repairing Complex Models with Topological Errors

【 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

Use Unity to publish APP to Hololens2 without pit tutorial

人人都在说的数据中台,你需要关注的核心特点是什么?

Ice Scorpion V4.0 attack, security dog products can be fully detected

毕设-基于SSM房屋租赁管理系统

引领数字医学高地,中山医院探索打造未来医院“新范式”

Bubble Sort and Quick Sort
随机推荐
IJCAI2022 | DictBert: Pre-trained Language Models with Contrastive Learning for Dictionary Description Knowledge Augmentation
MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
presto启动成功后出现2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer
From "useable" to "easy to use", domestic software is self-controllable and continues to advance
Likou - preorder traversal, inorder traversal, postorder traversal of binary tree
word分栏小记
How to find all fields with empty data in sql
[Filter tracking] based on matlab unscented Kalman filter inertial navigation + DVL combined navigation [including Matlab source code 2019]
队列题目:最近的请求次数
leetcode-每日一题1403. 非递增顺序的最小子序列(贪心)
undo problem
2022高处安装、维护、拆除考试题模拟考试题库及在线模拟考试
结构体初解
冰蝎V4.0攻击来袭,安全狗产品可全面检测
2022-08-04 第六小组 瞒春 学习笔记
word column notes
public static
List asList(T... a) What is the prototype? dmp (dump) dump file
用CH341A烧录外挂Flash (W25Q16JV)
21天学习挑战赛(2)图解设备树的使用