当前位置:网站首页>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 sort multiple fields and multiple values in sql statement
- QT language file production
- How to solve the error cannot update secondary snapshot during a parallel operation when the PostgreSQL database uses navicat to open the table structure?
- Kubernetes 网络入门
- A small tool to transfer files using QR code - QFileTrans 1.2.0.1
- 2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer appears after successful startup of presto
- 链表的简单描述及代码的简单实现
- 调用阿里云oss和sms服务
- leetcode - a subtree of another tree
- public static
List asList(T... a) What is the prototype?
猜你喜欢

沃谈小知识 |“远程透传”那点事儿

Beyond YOLO5-Face | YOLO-FaceV2 officially open source Trick+ academic point full

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

shell脚本:for循环与while循环

Bubble Sort and Quick Sort

结构体初解

如何在WordPress中添加特定类别的小工具

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

word column notes

Simple description of linked list and simple implementation of code
随机推荐
引领数字医学高地,中山医院探索打造未来医院“新范式”
Linux下常见的开源数据库,你知道几个?
word column notes
mysql can't Execute, please solve it
How to Add Category-Specific Widgets in WordPress
public static
List asList(T... a) What is the prototype? Native js realizes the effect of selecting and canceling all the multi-select boxes
ffmpeg 枚举decoders, encoders 分析
Use @Mapper to query the partition status of oracle and report an error
Hash table lookup (hash table)
Turn: Charles Handy: Who you are is more important than what you do
从“能用”到“好用” 国产软件自主可控持续推进
This year's Qixi Festival, "love vegetables" are more loving than gifts
undo problem
【七夕节】浪漫七夕,代码传情。将爱意变成绚烂的立体场景,给她(他)一个惊喜!(送代码)
How to simulate the background API call scene, very detailed!
使用二维码传输文件的小工具 - QFileTrans 1.2.0.1
Cybersecurity and the Metaverse: Identifying Weak Links
Everyone in China said data, you need to focus on core characteristic is what?
开发Hololens遇到The type or namespace name ‘HandMeshVertex‘ could not be found..