当前位置:网站首页>区间问题 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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 对for(var i = 0;i < 5;i++) {setTimeout(() => console.log(i),1000)}的深入分析
- 数据可视化图表总结(一)
- Chapter 6 relational database theory
- 开源存储这么香,为何我们还要坚持自研?
- 【Rust 笔记】15-字符串与文本(上)
- __ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
- Matrixdb V4.5.0 was launched with a new mars2 storage engine!
- 【LeetCode】Day95-有效的数独&矩阵置零
- Niu Mei's math problems
- 2021apmcm post game Summary - edge detection
猜你喜欢
Appium基础 — 使用Appium的第一个Demo
QQ电脑版取消转义符输入表情
背包问题 AcWing 9. 分组背包问题
MySQL advanced part 2: SQL optimization
数据可视化图表总结(二)
SQL三种连接:内连接、外连接、交叉连接
NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长
Is it impossible for lamda to wake up?
Overview of variable resistors - structure, operation and different applications
随机推荐
Traversal of leetcode tree
One question per day 1020 Number of enclaves
Quickly use Amazon memorydb and build your own redis memory database
LVS简介【暂未完成(半成品)】
Leetcode-9: palindromes
MySQL advanced part 2: storage engine
Shutter web hardware keyboard monitoring
[leetcode] day94 reshape matrix
Filter the numbers and pick out even numbers from several numbers
【Rust 笔记】15-字符串与文本(上)
New title of module a of "PanYun Cup" secondary vocational network security skills competition
leetcode-31:下一个排列
RGB LED infinite mirror controlled by Arduino
C Primer Plus Chapter 15 (bit operation)
1.15 - input and output system
leetcode-6108:解密消息
博弈论 AcWing 891. Nim游戏
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
Appium foundation - use the first demo of appium
Presentation of attribute value of an item