当前位置:网站首页>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;
}
边栏推荐
- Use SuperMap iDesktopX data migration tool to migrate map documents and symbols
- 你要的七夕文案,已为您整理好!
- 1667. 修复表中的名字
- Step by step how to perform data risk assessment
- 优炫数据库的单节点如何转集群
- High Item 02 Information System Project Management Fundamentals
- After the large pixel panorama is completed, what are the promotion methods?
- The problem of lack of dynamic library "libtinfo.so.5" in ksql application under UOS system
- 静态方法获取配置文件数据
- leetcode - symmetric binary tree
猜你喜欢

IJCAI2022 | DictBert: Pre-trained Language Models with Contrastive Learning for Dictionary Description Knowledge Augmentation

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

How to Add Category-Specific Widgets in WordPress

告白数字化转型时代,时速云镌刻价值新起点

北斗三号短报文终端露天矿山高边坡监测方案

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

Everyone in China said data, you need to focus on core characteristic is what?

论治理与创新,2022 开放原子全球开源峰会 OpenAnolis 分论坛圆满落幕

QT语言文件制作

mysql can't Execute, please solve it
随机推荐
Note that Weifang generally needs to pay attention to issuing invoices
Intersection of Boolean Operations in SuperMap iDesktop.Net - Repairing Complex Models with Topological Errors
word分栏小记
mysql没法Execute 大拿们求解
Likou - preorder traversal, inorder traversal, postorder traversal of binary tree
Programmer's Tanabata Romantic Moment
UOS系统下ksql应用缺少动态库”libtinfo.so.5“问题
Images using redis cache Linux master-slave synchronization server hard drive full of moved to the new directory which points to be modified
Compressed storage of special matrices
使用二维码传输文件的小工具 - QFileTrans 1.2.0.1
dmp (dump) dump file
如何在WordPress中添加特定类别的小工具
剑指Offer--找出数组中重复的数字(三种解法)
优炫数据库的单节点如何转集群
private封装
private package
The usage of try...catch and finally in js
百日刷题计划 ———— DAY2
Snapback - same tree
1667. 修复表中的名字