当前位置:网站首页>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;
}
边栏推荐
- 【 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
- Distributed systems revisited: there will never be a perfect consistency scheme...
- [Storage] Dawning Storage DS800-G35 ISCSI maps each LUN to the server
- How Jin Cang database correctness verification platform installation file
- Cybersecurity and the Metaverse: Identifying Weak Links
- Use SuperMap iDesktopX data migration tool to migrate ArcGIS data
- One hundred - day plan -- -- DAY2 brush
- The pit of std::string::find return value
- 通过模拟Vite一起深入其工作原理
- 【已解决】Unity Coroutinue 协程未有效执行的问题
猜你喜欢
Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme
public static <T> List<T> asList(T... a) 原型是怎么回事?
QT MV\MVC structure
Matlab drawing 3
为什么pca分量没有关联
运维监控系统之Open-Falcon
论治理与创新,2022 开放原子全球开源峰会 OpenAnolis 分论坛圆满落幕
【 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
Web3.0 Dapps——通往未来金融世界的道路
Is your data safe in this hyperconnected world?
随机推荐
ffmpeg pixel format basics
Principle and Technology of Virtual Memory
YYGH-13-客服中心
AI+PROTAC | dx/tx completes $5 million seed round
Everyone in China said data, you need to focus on core characteristic is what?
ASP.NET应用程序--Hello World
shell脚本:for循环与while循环
达梦8数据库导出导入
【七夕节】浪漫七夕,代码传情。将爱意变成绚烂的立体场景,给她(他)一个惊喜!(送代码)
Thinking (88): Use protobuf custom options for multi-version management of data
Hash table lookup (hash table)
Details such as compiling pretreatment
数学-求和符号的性质
21 Days Learning Challenge (2) Use of Graphical Device Trees
用Unity发布APP到Hololens2无坑教程
The usage of try...catch and finally in js
ffmpeg enumeration decoders, encoders analysis
The Tanabata copywriting you want has been sorted out for you!
Ice Scorpion V4.0 attack, security dog products can be fully detected
Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme