当前位置:网站首页>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;
}
边栏推荐
- Programmer's Tanabata Romantic Moment
- Dynamic management of massive service instances
- 数学-求和符号的性质
- word column notes
- Data storage practice based on left-order traversal
- Principle and Technology of Virtual Memory
- How to transfer a single node of Youxuan database to a cluster
- 【Daily Training】1403. Minimum Subsequence in Non-Increasing Order
- (十一)元类
- Linux下常见的开源数据库,你知道几个?
猜你喜欢
北斗三号短报文终端露天矿山高边坡监测方案
告白数字化转型时代,时速云镌刻价值新起点
J9 Digital Currency: What is the creator economy of web3?
Use SuperMap iDesktopX data migration tool to migrate map documents and symbols
Gantt chart is here, project management artifact, template is used directly
Never put off till tomorrow what you can put - house lease management system based on the SSM
ASP.NET application--Hello World
Cloud Native (32) | Introduction to Platform Storage System in Kubernetes
[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)
金仓数据库如何验证安装文件平台正确性
随机推荐
Bubble Sort and Quick Sort
HDU 1114:Piggy-Bank ← 完全背包问题
静态方法获取配置文件数据
HDU 1114: Piggy-Bank ← The Complete Knapsack Problem
Data to enhance Mixup principle and code reading
Ant Sword Advanced Module Development
从“能用”到“好用” 国产软件自主可控持续推进
Data storage practice based on left-order traversal
【已解决】Unity Coroutinue 协程未有效执行的问题
Is your data safe in this hyperconnected world?
【七夕节】浪漫七夕,代码传情。将爱意变成绚烂的立体场景,给她(他)一个惊喜!(送代码)
rpc-remote procedure call demo
A small tool to transfer files using QR code - QFileTrans 1.2.0.1
Use @Mapper to query the partition status of oracle and report an error
Hash table lookup (hash table)
Details such as compiling pretreatment
语法基础(变量、输入输出、表达式与顺序语句完成情况)
Flink 1.15.1 Cluster Construction (StandaloneSession)
Turn: Charles Handy: Who you are is more important than what you do
VSCode Change Default Terminal how to modify the Default Terminal VSCode