Phoenix is trying to take a photo of his n friends with labels 1, 2, ..., n who are…
- User
Phoenix is trying to take a photo of his n friends with labels 1, 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 i-th friend from the left had a label between a_i and b_i inclusive. Does there exist a unique way to order his friends based of his memory? Input
The first line contains one integer n (1 ≤ n ≤ 2⋅10^5) — the number of friends.
The i-th of the next n lines contain two integers a_i and b_i (1 ≤ a_i ≤ b_i ≤ n) — Phoenix's memory of the i-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 n integers — the i-th integer should be the label of the i-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
4 4 4 1 3 2 4 3 4
Output
YES 4 1 2 3
Input
4 1 3 2 4 3 4 2 3
Output
NO 1 3 4 2 1 2 4 3
Assistant
import sys from sys import stdin class DSU: def __init__(self, max_num): self.parent = list(range(max_num + 2)) # 0 to max_num+1 def find(self, x): if self.parent[x] != x: self.parent[x] = self.find(self.parent[x]) return self.parent[x] def mark_used(self, x): self.parent[x] = self.find(x + 1) def main(): n = int(stdin.readline()) intervals = [] for _ in range(n): a, b = map(int, stdin.readline().split()) intervals.append((a, b)) # Build permutation P using DSU and collect candidates dsu = DSU(n) P = [] candidates = [] for i in range(n): a, b = intervals[i] x = dsu.find(a) if x > b: print("PROBLEM! Input is invalid") return P.append(x) dsu.mark_used(x) next_x = dsu.find(x + 1) if next_x <= b: candidates.append(i) if not candidates: print("YES") print(' '.join(map(str, P))) return # Try each candidate to build Q for i in candidates: new_dsu = DSU(n) # Mark all numbers used in P[0..i-1] for j in range(i): x_j = P[j] new_dsu.mark_used(x_j) a_i, b_i = intervals[i] x_prime = new_dsu.find(a_i) if x_prime > b_i or x_prime == P[i]: continue # Check if x_prime is available if new_dsu.find(x_prime) != x_prime: continue # Mark x_prime as used and proceed new_dsu.mark_used(x_prime) Q = P[:i] + [x_prime] valid = True current_dsu = new_dsu for j in range(i+1, n): a, b = intervals[j] x = current_dsu.find(a) if x > b: valid = False break Q.append(x) current_dsu.mark_used(x) if valid and Q != P: print("NO") print(' '.join(map(str, P))) print(' '.join(map(str, Q))) return # If no Q found, output YES (but according to problem statement, there's at least one permutation) print("YES") print(' '.join(map(str, P))) if __name__ == "__main__": main()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.