当前位置:网站首页>905. 区间选点
905. 区间选点
2022-08-05 03:07: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(a,a+n,comp);
// 每次选择区间的右端点作为选点
int r = a[0].second;
int res = 1;
for(int i = 1; i < n; i++){
if(a[i].first > r){
//如果当前区间的左端点>选点 即区间不包括选点,则更新区间边界和新的选点
res++;
r = a[i].second;
}//如果当前区间的左端点<=选点,则忽略当前区间,继续判断下一个区间
}
cout << res << endl;
return 0;
}
边栏推荐
- sql server installation prompts that the username does not exist
- Never put off till tomorrow what you can put - house lease management system based on the SSM
- 毕设-基于SSM房屋租赁管理系统
- 使用二维码传输文件的小工具 - QFileTrans 1.2.0.1
- 1527. Patients suffering from a disease
- private package
- Syntax basics (variables, input and output, expressions and sequential statement completion)
- 627. Change of gender
- Data storage practice based on left-order traversal
- QStyle平台风格
猜你喜欢

龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入

Dynamic management of massive service instances

【软件测试】自动化测试之unittest框架

大像素全景制作完成后,推广方式有哪些?

.NET Application -- Helloworld (C#)

Talking about data security governance and privacy computing

QT MV\MVC结构

dmp(dump)转储文件

In 2022, you still can't "low code"?Data science can also play with Low-Code!

QT language file production
随机推荐
毕设-基于SSM房屋租赁管理系统
1484. Sell Products by Date
你要的七夕文案,已为您整理好!
21天学习挑战赛(2)图解设备树的使用
AI+PROTAC | dx/tx completes $5 million seed round
龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入
A small tool to transfer files using QR code - QFileTrans 1.2.0.1
QT MV\MVC structure
Turn: Charles Handy: Who you are is more important than what you do
sql server installation prompts that the username does not exist
dmp (dump) dump file
Summary of domestic environments supported by SuperMap
腾讯云【Hiflow】新时代自动化工具
leetcode - symmetric binary tree
22-07-31周总结
UOS系统下ksql应用缺少动态库”libtinfo.so.5“问题
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]
Apache DolphinScheduler, a new generation of distributed workflow task scheduling platform in practice - Medium
Gantt chart is here, project management artifact, template is used directly