当前位置:网站首页>Interval problem acwing 906 Interval grouping
Interval problem acwing 906 Interval grouping
2022-07-05 06:24:00 【T_ Y_ F666】
Interval problem AcWing 906. Interval grouping
Original link
Algorithm tags
greedy
Ideas
ans Minimum number of groups , The correct answer
cnt The legal scheme is obtained according to the following algorithm
so ans<=cnt
prove ans>=cnt
therefore ans==cnt
Code
#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);
// The small root heap maintains the maximum value of the right endpoint of all intervals int Interval subscript vector<int> The right end of the interval
priority_queue<int, vector<int>, greater<int>> heap;
rep(i, 0, n){
// Need to open up new areas
if(heap.empty()||heap.top()>=sec[i].l){
heap.push(sec[i].r);
}else{ // Update the maximum value of the right endpoint of the current interval
heap.pop();
heap.push(sec[i].r);
}
}
printf("%lld", heap.size());
return 0;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support 
边栏推荐
- C job interview - casting and comparing - C job interview - casting and comparing
- How to generate an image from text on fly at runtime
- Sword finger offer II 058: schedule
- Sum of three terms (construction)
- JS quickly converts JSON data into URL parameters
- Nested method, calculation attribute is not applicable, use methods
- MySQL advanced part 2: storage engine
- RecyclerView的应用
- Leetcode-6109: number of people who know secrets
- Filter the numbers and pick out even numbers from several numbers
猜你喜欢

Quickly use Amazon memorydb and build your own redis memory database

MySQL advanced part 2: SQL optimization

4. Object mapping Mapster

Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition

TCP's understanding of three handshakes and four waves

AE tutorial - path growth animation

Operator priority, one catch, no doubt

栈 AcWing 3302. 表达式求值

5. Oracle TABLESPACE

Redis-01.初识Redis
随机推荐
P3265 [jloi2015] equipment purchase
在新线程中使用Handler
Currently clicked button and current mouse coordinates in QT judgment interface
Leetcode-1200: minimum absolute difference
Liunx starts redis
5.Oracle-表空间
Shutter web hardware keyboard monitoring
C Primer Plus Chapter 15 (bit operation)
Winter messenger 2
博弈论 AcWing 894. 拆分-Nim游戏
Leetcode-6110: number of incremental paths in the grid graph
Filter the numbers and pick out even numbers from several numbers
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
Leetcode-9: palindromes
MySQL advanced part 2: SQL optimization
Leetcode recursion
Golang uses context gracefully
Suppose a bank's ATM machine, which allows users to deposit and withdraw money. Now there is 200 yuan in an account, and both user a and user B have the right to deposit and withdraw money from this a
5. Oracle TABLESPACE
Presentation of attribute value of an item