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

代码:

#include
#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 >vc[maxn];
struct node{
    int id,val;
}c[maxn<<1];
template 
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 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;
    pairtmp1,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 >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;
        pairk=query(1,1,n,i+1,y[p[i]]);
        if(k.first<=i){
            //cout<

 

你可能感兴趣的:(其他:细节处理,数据结构:贪心,数据结构:线段树/树状数组)