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
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
- Ural 2041 Nanomatryoshkas(贪心)
- URAL 2041 Nanomatryoshkas(贪心)
- ural 1820. Ural Steaks 贪心
- URAL 1389 Roadworks 贪心
- URAL 1826. Minefield 贪心
- URAL 1820 Ural Steaks (贪心)
- ural 1303 Minimal Coverage【贪心】
- URAL 1901 Space Elevators(贪心)
- URAL 1303. Minimal Coverage 贪心
- URAL 1203 Scientific Conference (贪心)
- URAL 1303 Minimal Coverage (贪心)
- URAL 1826 贪心经典题型
- URAL 1964 - Chinese Dialects(贪心)
- URAL 1203 Scientific Conference(贪心 || DP)
- URAL 1743 Domino Sorting 排序 + 贪心
- Ural 1303 Minimal Coverage(贪心)
- URAL 2021 Scarily interesting!(贪心)
- URAL 2000 Grand Theft Array V 贪心
- 数据结构实验之二叉树的建立与遍历
- 111
- App系列------如何把文件放到assets目录下
- [DP]Composite设计模式
- Docker学习
- Ural 2041 Nanomatryoshkas(贪心)
- Python Requests throwing up SSLError
- 树形DPpoj2152
- 设计模式-行为型-观察者模式(Observer)
- 并查集
- 移动前端知识总结
- Linux 下安装erlang
- WCF大数据量处理
- 对Volatile变量的理解和总结