题意:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/10e246cd5cf823c5be059333931fad5b.png)
解法:
贪心
对于[1,1e9]的每个盒子i,
能放入的球必须满足l<=i,如果有多个满足条件的球,
那么显然优先考虑选择r较小的球放入.
那么能想到一个暴力的做法:
枚举盒子i,
将满足l<=i的所有球的r丢入堆中,
那么每次就选择堆顶放入盒子i中.
盒子数是1e9级别的,直接for循环肯定不行,
观察球的l和r是离散的,因此放的盒子的编号也一定是离散的的,
当堆中没有球的时候,二分将盒子编号i跳到下一个有球的位置就行了.
总复杂度O(n*log).
code:
#include<bits/stdc++.h>
using namespace std;
map<int,vector<int> >mp;
set<int>idx;
int n;
void init(){
mp.clear();
idx.clear();
}
void solve(){
init();
cin>>n;
for(int i=1;i<=n;i++){
int l,r;cin>>l>>r;
mp[l].push_back(r);
idx.insert(l);
}
idx.insert(1e9+1);
priority_queue<int,vector<int>,greater<int> >q;
int now=1;
while(now<=1e9){
if(mp.count(now)){
for(auto &i:mp[now]){
q.push(i);
}
}
if(q.empty())now=*idx.lower_bound(now);
else{
int x=q.top();q.pop();
if(x<now){
cout<<"NO"<<endl;return ;
}
now++;
}
}
if(q.size()){
cout<<"NO"<<endl;return ;
}
cout<<"YES"<<endl;
}
signed main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
ios::sync_with_stdio(0);cin.tie(0);
int T;cin>>T;while(T--)
solve();
return 0;
}