当前位置:网站首页>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;
}
边栏推荐
- 告白数字化转型时代,时速云镌刻价值新起点
- Bubble Sort and Quick Sort
- Open Source License Description LGPL
- Distributed systems revisited: there will never be a perfect consistency scheme...
- rpc-remote procedure call demo
- Hash table lookup (hash table)
- Study Notes-----Left-biased Tree
- Use SuperMap iDesktopX data migration tool to migrate map documents and symbols
- Matlab drawing 3
- public static <T> List<T> asList(T... a) 原型是怎么回事?
猜你喜欢

基于生长的棋盘格角点检测方法

Dynamic management of massive service instances

The linear table lookup

Question about #sql shell#, how to solve it?

用CH341A烧录外挂Flash (W25Q16JV)

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

Study Notes-----Left-biased Tree

tree table lookup

ASP.NET application--Hello World

QT MV\MVC structure
随机推荐
Ffmpeg - sources analysis
AI+PROTAC | dx/tx completes $5 million seed round
Ice Scorpion V4.0 attack, security dog products can be fully detected
语法基础(变量、输入输出、表达式与顺序语句)
(十一)元类
龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入
21天学习挑战赛(2)图解设备树的使用
Why did they choose to fall in love with AI?
Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme
Developing Hololens encountered The type or namespace name 'HandMeshVertex' could not be found..
[Storage] Dawning Storage DS800-G35 ISCSI maps each LUN to the server
How to Add Category-Specific Widgets in WordPress
2022-08-04 The sixth group, hidden from spring, study notes
Use SuperMap iDesktopX data migration tool to migrate ArcGIS data
Syntax basics (variables, input and output, expressions and sequential statements)
毕设-基于SSM房屋租赁管理系统
Matlab drawing 3
Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals
Call Alibaba Cloud oss and sms services
QT MV\MVC structure