当前位置:网站首页>区间问题 AcWing 906. 区间分组
区间问题 AcWing 906. 区间分组
2022-07-27 10:35: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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- An article reveals the NFT strategy of traditional game manufacturers such as Ubisoft
- Real time development platform construction practice, in-depth release of real-time data value - 04 live broadcast review
- 迭代次数和熵之间关系的一个验证试验
- Why is the data service API the standard configuration of the data midrange when we take the last mile of the data midrange?
- Use__ slots__ And__ dict__ To save space (it's simply a qualitative leap, and leetcode's personal test is effective)
- Analysis of new communication security risks brought by quantum computer and Countermeasures
- Application scenarios, key technologies and network architecture of communication perception integration
- Use of pyquery
- The open source project Taier version 1.1 was officially released, and the list of new functions is fast
- KEPServer配置
猜你喜欢

Shortest moving distance and entropy of morphological complex

Chengying, kangaroo cloud one-stop fully automated operation and maintenance steward, is officially open source

The second method of calculating overlapping integral

Instructions for mock platform

Openatom openharmony sub forum, see you today at 14:00! Wonderful release of memorabilia attached

Budweiser, a well-known beer, plans to launch NFT in an attempt to unveil the "long planned" uplink?

最长上升子序列模型 AcWing 1014. 登山

背包模型 AcWing 1024. 装箱问题

背包模型 AcWing 1022. 宠物小精灵之收服

背包模型 AcWing 423. 采药
随机推荐
最长上升子序列模型 AcWing 1012. 友好城市
Budweiser, a well-known beer, plans to launch NFT in an attempt to unveil the "long planned" uplink?
What is the mystery of the gate of the meta universe?
ACM warm-up Exercise 2 in 2022 summer vacation (summary)
Kangaroo cloud stack based on CBO in spark SQL optimization
推导STO双中心动能积分的详细展开式
Use of beautifulsoup
Analysis of new communication security risks brought by quantum computer and Countermeasures
最长上升子序列模型 AcWing 272. 最长公共上升子序列
洛谷P1441 砝码称重
Yum source installation
Taishan Office Technology Lecture: scaling and opening files
Yonbuilder enables innovation, and the "golden keyboard Award" of the fourth UFIDA developer competition is open!
学习笔记-uni-app
Redis high availability principle
2022牛客多校 (3)J.Journey
迭代次数和熵之间关系的一个验证试验
How to build a data index system is the most effective. From 0 to 1, we will take you a quick start - 02 live review
图片中非0值的数量对分类的影响
2022牛客多校训练(3)A-Ancestor 题目翻译