905. 区间选点 - AcWing题库
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct Range
{
int l,r;
bool operator <(const Range&m)const
{
return r<m.r;
}
}range[N];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>range[i].l>>range[i].r;
}
sort(range,range+n);
int l=0,r=-0x3f3f3f3f;
int res=0;
for(int i=0;i<n;i++)
{
if(r>=range[i].l)
{
continue;
}
else
{
r=range[i].r;
res++;
}
}
cout<<res;
}
906. 区间分组 - AcWing题库
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct Range{
int l,r;
bool operator <(const Range &m)const
{
return l<m.l;
}
}range[N];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>range[i].l>>range[i].r;
}
sort(range,range+n);
priority_queue<int ,vector<int>,greater<int>>q;
q.push(range[0].r);
int res=1;
for(int i=1;i<n;i++)
{ int x;
x=q.top();
if(range[i].l<=x)
{
res++;
q.push(range[i].r);
}
else
{
q.pop();
q.push(range[i].r);
}
}
cout<<res;
}
907. 区间覆盖 - AcWing题库
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct Range
{
int l,r;
bool operator <(const Range&m)const
{
return l<m.l;
}
}range[N];
int main()
{
int be,end;
cin>>be>>end;
int n;cin>>n;
for(int i=0;i<n;i++)
{
cin>>range[i].l>>range[i].r;
}
sort(range,range+n);
int res=0;
int check=0;
for(int i=0;i<n;i++)
{ int j=i,r=-2e9;
while(j<n&&range[j].l<=be)
{
r=max(r,range[j].r);
//cout<<r<<endl;
j++;
}
// cout<<endl;
if(r==-2e9)
{
check=0;
break;
}
res++;
//cout<<r<<endl;
i=j-1;
be=r;
if(be>=end)
{
check=1;
break;
}
}
if(check) cout<<res;
else cout<<-1;
}