time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Phoenix is trying to take a photo of his nn friends with labels 1,2,…,n1,2,…,n who are lined up in a row in a special order. But before he can take the photo, his friends get distracted by a duck and mess up their order.
Now, Phoenix must restore the order but he doesn't remember completely! He only remembers that the ii-th friend from the left had a label between aiai and bibi inclusive. Does there exist a unique way to order his friends based of his memory?
Input
The first line contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of friends.
The ii-th of the next nn lines contain two integers aiai and bibi (1≤ai≤bi≤n1≤ai≤bi≤n) — Phoenix's memory of the ii-th position from the left.
It is guaranteed that Phoenix's memory is valid so there is at least one valid ordering.
Output
If Phoenix can reorder his friends in a unique order, print YES followed by nn integers — the ii-th integer should be the label of the ii-th friend from the left.
Otherwise, print NO. Then, print any two distinct valid orderings on the following two lines. If are multiple solutions, print any.
Examples
input
Copy
4 4 4 1 3 2 4 3 4
output
Copy
YES 4 1 2 3
input
Copy
4 1 3 2 4 3 4 2 3
output
Copy
NO 1 3 4 2 1 2 4 3
題意:
有n(n<=2e5)個人和n個位置,對人編號1~n。接下來每個位置給你一個區間[l,r],表示編號在[l,r]區間內的人可以選擇這個位置。
每個人只能選一個位置,每個位置也只能被一個人選。保證合法的選擇方案一定存在,現在問你是否唯一,如果唯一輸出YES和每個位置對應的人的編號,否則輸出NO並且任意輸出兩種合法的方案。
思路:
由於保證答案存在,因此我們可以直接從1號人開始貪心選擇他能選擇的位置中,區間右端點最小的那個位置。這樣下來n個人都能選到一個合法的位置。至於是否唯一呢,我們需要觀察發現,設編號爲i的人此時選擇的位置爲pi,則若
l[p[j]]<=i<j<=r[p[i]] ,那麼i和j的位置就可以交換。繼續推導,發現編號爲i的人,只要找到編號位於區間[ i+1,r[p[i]] ]的人j,並且j所處的位置的左端點l[p[j]]<=i,那麼編號爲i,j的人就可以互換位置。因此可以建一顆線段樹,保存每個人所在的位置以及其位置的左端點,只做查詢,如果有一個滿足條件的直接交換就好了。(主要還是理清思路,不要被題目繞進去)
代碼:
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define lson (rt<<1)
#define rson (rt<<1|1)
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
using namespace std;
const int maxn=4e5+5;
//const double pi=acos(-1.0);
//const double eps=1e-9;
//const ll mo=1e9+7;
int n,m,k;
int a[maxn],p[maxn],x[maxn],y[maxn];
int tmp,cnt;
int flag;
int ans[maxn];
vector<pair<int,int> >vc[maxn];
struct node{
int id,val;
}c[maxn<<1];
template <typename T>
inline void read(T &X)
{
X=0;int w=0; char ch=0;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
if(w) X=-X;
}
void build(int rt,int l,int r){
if(l==r){
c[rt].id=p[l];
c[rt].val=x[p[l]];
return ;
}
int mid=(l+r)>>1;
build(lson,l,mid);
build(rson,mid+1,r);
if(c[lson].val<c[rson].val) c[rt]=c[lson];
else c[rt]=c[rson];
}
pair<int,int> query(int rt,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr){
return make_pair(c[rt].val,c[rt].id);
}
int mid=(l+r)>>1;
pair<int,int>tmp1,tmp2;tmp1.first=-1;tmp2.first=-1;
if(ql<=mid) tmp1=query(lson,l,mid,ql,qr);
if(qr>mid) tmp2=query(rson,mid+1,r,ql,qr);
if(tmp1.first==-1) return tmp2;
if(tmp2.first==-1) return tmp1;
if(tmp1.first>tmp2.first) return tmp2;
return tmp1;
}
void prt(){
rep(i,1,n)
printf("%d%c",ans[i],i==n?'\n':' ');
}
int main()
{
read(n);
rep(i,1,n){
int l,r;
read(l);read(r);
x[i]=l;y[i]=r;
vc[l].push_back(make_pair(r,i));
}
set<pair<int,int> >st;
rep(i,1,n){
st.insert(vc[i].begin(),vc[i].end());
int y=(*st.begin()).second;
st.erase(st.begin());
ans[y]=i;
p[i]=y;
}
build(1,1,n);
rep(i,1,n){
if(i+1>y[p[i]]) continue;
pair<int,int>k=query(1,1,n,i+1,y[p[i]]);
if(k.first<=i){
//cout<<i<<" "<<k.second<<endl;
puts("NO");
prt();
swap(ans[p[i]],ans[k.second]);
prt();
return 0;
}
}
puts("YES");
prt();
return 0;
}