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