Ural 2041 Nanomatryoshkas(贪心)

来源:互联网 发布:java 数据库连接池 编辑:程序博客网 时间:2024/06/11 16:59
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 ≤ 105) which is the number of nanomatryoshkas.Nextn 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 106.

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, printn 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
31 52 26 7
Yes3 1 2 
32 22 23 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
原创粉丝点击