Learn the Tricks
Practice
0 (0 votes)
Sorting
Greedy algorithms
Problem
22% Success 105 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

There is a school of magic where 109 students study, each student has a distinct number ID from 1 to 109. There is a teacher who teaches magic to children. The teacher knows n types of tricks that he wants to teach, but since the number of students are large, he teaches the ith trick to only a range of students whose number ID lie in the range [l,r] (l and r included). You couldnt take admission into the school but want to learn all the n tricks. To do so you ask a student who knows ith trick to teach you for some amount of money. Since you want to learn all the tricks and save money you want to select the minimum number of students you need to chose that will teach you all the tricks. Since there are multiple possible answers, print the lexicographically smallest answer.

Input

  • First line contains n, the number of tricks the teacher knows.
  • N lines follow, ith line contain 2 space seperated integers l and r, the range of students who are taught the ith trick.

Output

  • In first line print the minimum number of students that you need to choose.
  • In next line print the ID of each selected student space seperated. The answer should be lexicographically smallest

Constraints

  • 1\(\leq\) n \(\leq\) 105
  • 1\(\leq\) l \(\leq\) r \(\leq\) 109

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
6 votes
Tags:
SortingAlgorithmsAdvanced Sorting
Points:30
7 votes
Tags:
AlgorithmsMedium
Points:30
1 votes
Tags:
MediumArraysSortingAlgorithmsSetSorting
Editorial

No editorial available for this problem.