当前位置:网站首页>区间问题 AcWing 906. 区间分组
区间问题 AcWing 906. 区间分组
2022-07-05 06:16:00 【T_Y_F666】
区间问题 AcWing 906. 区间分组
原题链接
算法标签
贪心
思路
ans 最小组数, 即正确答案
cnt 按照如下算法所得合法方案
故ans<=cnt
证明ans>=cnt
所以 ans==cnt
代码
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>b;--i)
using namespace std;
const int N = 100005;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
struct Sec{
int l, r;
}sec[N];
int mxr[N];
bool cmp(Sec A, Sec B){
return A.l<B.l;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n=read();
rep(i, 0, n){
sec[i].l=read(), sec[i].r=read();
}
sort(sec, sec+n,cmp);
// 小根堆维护所有区间右端点最大值 int 区间下标 vector<int> 区间右端点
priority_queue<int, vector<int>, greater<int>> heap;
rep(i, 0, n){
// 需要开辟新区间
if(heap.empty()||heap.top()>=sec[i].l){
heap.push(sec[i].r);
}else{ // 更新当前区间右端点最大值
heap.pop();
heap.push(sec[i].r);
}
}
printf("%lld", heap.size());
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Doing SQL performance optimization is really eye-catching
- [2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian
- MySQL advanced part 1: triggers
- Operator priority, one catch, no doubt
- Sum of three terms (construction)
- 中国剩余定理 AcWing 204. 表达整数的奇怪方式
- [rust notes] 13 iterator (Part 2)
- Daily question 1189 Maximum number of "balloons"
- MySQL advanced part 2: the use of indexes
- Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
猜你喜欢

SPI details

leetcode-6111:螺旋矩阵 IV

Leetcode-6111: spiral matrix IV

栈 AcWing 3302. 表达式求值

Is it impossible for lamda to wake up?

Appium自动化测试基础 — Appium测试环境搭建总结

博弈论 AcWing 891. Nim游戏

Data visualization chart summary (I)
![[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields](/img/65/7fa32cd0005ddaaebacd85c25e0c7e.jpg)
[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields

MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
随机推荐
Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
Data visualization chart summary (I)
js快速将json数据转换为url参数
How to generate an image from text on fly at runtime
Golang uses context gracefully
Appium foundation - use the first demo of appium
数据可视化图表总结(二)
Sum of three terms (construction)
Niu Mei's math problems
Chapter 6 relational database theory
Overview of variable resistors - structure, operation and different applications
leetcode-22:括号生成
【LeetCode】Easy | 20. Valid parentheses
Nested method, calculation attribute is not applicable, use methods
Usage scenarios of golang context
【LeetCode】Day95-有效的数独&矩阵置零
CPU内核和逻辑处理器的区别
C - XOR to all (binary topic)
Regulations for network security events of vocational group in 2022 Guizhou Vocational College skill competition
4. Object mapping Mapster