当前位置:网站首页>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;
}
边栏推荐
- lua learning
- word分栏小记
- Matlab画图3
- 使用二维码传输文件的小工具 - QFileTrans 1.2.0.1
- 【滤波跟踪】基于matlab无迹卡尔曼滤波惯性导航+DVL组合导航【含Matlab源码 2019期】
- 2022-08-04 The sixth group, hidden from spring, study notes
- Dynamic management of massive service instances
- QT MV\MVC structure
- Likou - preorder traversal, inorder traversal, postorder traversal of binary tree
- Distributed systems revisited: there will never be a perfect consistency scheme...
猜你喜欢

tree table lookup

Study Notes-----Left-biased Tree

Is your data safe in this hyperconnected world?

A small tool to transfer files using QR code - QFileTrans 1.2.0.1

2022 High-level installation, maintenance, and removal of exam questions mock exam question bank and online mock exam

.NET应用程序--Helloworld(C#)

word column notes

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

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

你要的七夕文案,已为您整理好!
随机推荐
The 20th day of the special assault version of the sword offer
High Item 02 Information System Project Management Fundamentals
思考(八十八):使用 protobuf 自定义选项,做数据多版本管理
冒泡排序与快速排序
[Storage] Dawning Storage DS800-G35 ISCSI maps each LUN to the server
21天学习挑战赛(2)图解设备树的使用
1527. Patients suffering from a disease
Use @Mapper to query the partition status of oracle and report an error
1873. The special bonus calculation
百日刷题计划 ———— DAY2
Question about #sql shell#, how to solve it?
数学-求和符号的性质
Snapback - same tree
CPDA|How Operators Learn Data Analysis (SQL) from Negative Foundations
如何在WordPress中添加特定类别的小工具
mysql没法Execute 大拿们求解
Physical backup issues caused by soft links
How OpenGL works
汉字转拼音
The problem of lack of dynamic library "libtinfo.so.5" in ksql application under UOS system