H—井然有序之窗
题目链接:https://ac.nowcoder.com/acm/contest/95323/H
题意:
给定n个区间,要求填入每个下标的数字在区间范围内以此来构建排列
思路:
先按照左端点进行区间的排序,然后将每个左端点小于等于当前数字的区间压入优先队列
由于右端点大的能给以后操作留出更多的空间,所以优先队列是按照右端点升序排列的(操作空间小的先填上)
如果当前数字大于队首区间的右端点说明无论如何也构建不了排列 f=false
如果当前数字没有相应的区间,即优先队列为空也构建不了 f=false
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const ll llmax=LLONG_MAX;
const int maxn=1e5+5;
struct node{
int l;
int r;
int index;
bool operator<(const node &a)const {
if(l!=a.l){
return l<a.l;
}
return r<a.r;
}
};
struct x{
int r;
int index;
bool operator<(const x&a)const{
return r>a.r;
}
};
vector<node>seg;
int ans[maxn];
signed main()
{
ios::sync_with_stdio(false),cin.tie(0);
int n;cin>>n;
for(int i=1;i<=n;i++){
int l,r;cin>>l>>r;
seg.emplace_back(node{l,r,i});
}
sort(seg.begin(),seg.end());
// for(int i=0;i<n;i++){
// cout<<seg[i].l<<' '<<seg[i].r<<endl;
// }
int ptr=0;
int k=1;
bool f=true;
priority_queue<x>q;
while(ptr<=n-1||k!=n+1){
if(ptr<=n-1)
{
while(ptr<=n-1&&seg[ptr].l<=k){
q.push((x){seg[ptr].r,seg[ptr].index});
ptr++;
}
}
if(!q.empty()){
x now=q.top();
if(k<=now.r){
ans[now.index]=k;
q.pop();
k++;
}else{
f=false;
break;
}
}else{
f=false;
}
if(f==false)break;
}
if(f)for(int i=1;i<=n;i++){
cout<<ans[i]<<' ';
}else{
cout<<-1;
}
return 0;
}

浙公网安备 33010602011771号