Ural 2041 Nanomatryoshkas(贪心)

116 篇文章 0 订阅
65 篇文章 0 订阅
Matryoshka is a traditional Russian recursive doll. But everything changes, and even matryoshka needs a little innovation.Due to the use of new materials, it became possible to make a matryoshka arbitrarily thin without decreasing its durability.Soon, these new nanomatryoshkas filled the market. Now, salesman Alexander has a problem: he needs to place all nanomatryoshkason a shelf in his shop.
Each nanomatryoshka has an internal volume and an external volume. One nanomatryoshka fits into another if the external volume of the first onedoes not exceed the internal volume of the second one. Alexander is sure that nanomatryoshkas should be placed in a row so that no nanomatryoshka (except the last one) fits into the next one in the row.Help Alexander, and he might give you a discount for a couple of nanomatryoshkas!

Input

The first line contains an integer n (2 ≤ n ≤ 10 5) which is the number of nanomatryoshkas.Next n lines contain two integers each: internal and external volumes of a corresponding nanomatryoshka.It is guaranteed that the internal volume of each nanomatryoshka never exceeds the external volume, but they can be equal. Both numbers are in range from 1 to 10 6.

Output

If it is impossible to place nanomatryoshkas in the described order, print “No”.Otherwise, on the first line, print “Yes”, and on the second line, print n integers: the numbers of nanomatryoshkas in their order on the shelf.Nanomatryoshkas are numbered starting from one in the order of their appearance in the input file. If there are several solutions, print any of them.

Samples

inputoutput
3
1 5
2 2
6 7
Yes
3 1 2 
3
2 2
2 2
3 4
No
 


题意:给你N个俄罗斯套娃, 每个套娃有外体积和一个内体积,问你能不能找到一个排列使得每个套娃都盖不住它前边的那个。


分析:先按外体积排序再按内体积排序,不符合条件的只能是那些内外体积相等的,这些娃娃我们用set统一处理。


WA了好处几次,都是在细节判断上有问题,continue和逻辑判断需谨慎。

    #include<iostream>  
    #include<string>  
    #include<algorithm>  
    #include<cstdlib>  
    #include<cstdio>  
    #include<set>  
    #include<map>  
    #include<vector>  
    #include<cstring>  
    #include<stack>  
    #include<cmath>  
    #include<queue>  
    #define INF 0x3f3f3f3f  
    #define eps 1e-9  
    #define MAXN 100005  
    using namespace std;  
    int n,l[MAXN],r[MAXN];  
    struct Toy  
    {  
        int in,out,num;  
        friend bool operator < (Toy a,Toy b)  
        {  
            if(a.out == b.out) return a.in > b.in;  
            return a.out > b.out;  
        }  
    }toy[MAXN];  
    struct thing  
    {  
        int val,num;  
        thing(int x,int y)  
        {  
            val = x,num = y;  
        }  
        friend bool operator < (thing a,thing b)  
        {  
            return a.val > b.val;  
        }  
    };  
    void Insert(int x,int y)  
    {  
        r[l[y]] = x;  
        l[x] = l[y];  
        r[x] = y;  
        l[y] = x;  
    }  
    multiset <thing> s;  
    int main()  
    {  
        scanf("%d",&n);  
        for(int i = 1;i <= n;i++)   
        {  
            scanf("%d%d",&toy[i].in,&toy[i].out);  
            toy[i].num = i;  
        }  
        int flag = false;  
        l[0] = r[0] = 0;   
        sort(toy+1,toy+1+n);  
        for(int i = 1;i <= n;i++)  
        {  
            if(toy[i].in != toy[i].out)  Insert(toy[i].num,0);
            else   
            if(toy[i-1].in != toy[i].in || toy[i-1].in != toy[i-1].out)  
            {   
                Insert(toy[i].num,0);
                int j = i;  
                for(;toy[j].in == toy[j].out && toy[j].in == toy[i].in;j++);  
                j--;  
                if(i != j)  
                {  
                    while(s.size() && s.begin()->val >= toy[i].out) s.erase(s.begin());  
                    if(s.size() < j-i)  
                    {  
                        flag = true;  
                        break;  
                    }  
                    multiset <thing> :: iterator it = s.begin();  
                    while(j > i)  
                    {  
                        Insert(toy[j].num,it->num);  
                        it++;  
                        j--;  
                    }  
                }  
            }  
            s.insert(thing(toy[i].in,toy[i].num));  
        }  
        if(flag)  
        {  
            cout<<"No"<<endl;  
            return 0;  
        }  
        cout<<"Yes"<<endl;  
        for(int i = r[0];i;i = r[i]) cout<<i<<" ";  
    }  


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值