Codeforces Round #638 (Div. 2) F. Phoenix and Memory(贪心+线段树)

F. Phoenix and Memory

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值