当前位置:网站首页>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;
}
边栏推荐
- How to find all fields with empty data in sql
- Common open source databases under Linux, how many do you know?
- 调用阿里云oss和sms服务
- 21 Days Learning Challenge (2) Use of Graphical Device Trees
- Turn: Charles Handy: Who you are is more important than what you do
- 905. 区间选点
- QT MV\MVC structure
- High Item 02 Information System Project Management Fundamentals
- 大像素全景制作完成后,推广方式有哪些?
- Intersection of Boolean Operations in SuperMap iDesktop.Net - Repairing Complex Models with Topological Errors
猜你喜欢
Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals
On governance and innovation, the 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum came to a successful conclusion
Use SuperMap iDesktopX data migration tool to migrate ArcGIS data
[Filter tracking] based on matlab unscented Kalman filter inertial navigation + DVL combined navigation [including Matlab source code 2019]
通过模拟Vite一起深入其工作原理
冒泡排序与快速排序
使用二维码传输文件的小工具 - QFileTrans 1.2.0.1
龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入
[Solved] Unity Coroutine coroutine is not executed effectively
Use Unity to publish APP to Hololens2 without pit tutorial
随机推荐
语法基础(变量、输入输出、表达式与顺序语句)
用CH341A烧录外挂Flash (W25Q16JV)
mysql can't Execute, please solve it
Slapped in the face: there are so many testers in a certain department of byte
.NET Application -- Helloworld (C#)
冰蝎V4.0攻击来袭,安全狗产品可全面检测
通过模拟Vite一起深入其工作原理
十五. 实战——mysql建库建表 字符集 和 排序规则
Physical backup issues caused by soft links
Linux下常见的开源数据库,你知道几个?
The usage of try...catch and finally in js
Web3.0 Dapps——通往未来金融世界的道路
sql server 安装提示用户名不存在
On governance and innovation, the 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum came to a successful conclusion
Everyone in China said data, you need to focus on core characteristic is what?
Summary of domestic environments supported by SuperMap
This year's Qixi Festival, "love vegetables" are more loving than gifts
21天学习挑战赛(2)图解设备树的使用
CPDA|How Operators Learn Data Analysis (SQL) from Negative Foundations
Likou - preorder traversal, inorder traversal, postorder traversal of binary tree