spoj#SELLINGA. Selling Art
Selling Art
To the best of her friends’ knowledge, Irina is a great artist. That is why she decided to expose her work and conquer the world. Well, just sell it alright. To help her, her friends executed a decent strategy. For a whole month they went to “The Manége”, an arts exhibition hall in Saint Petersburg. They wrote down on a piece of paper the times at which collectionists enter and leave. Then they consolidated the data and realized that the times at which collectionists enter and leave is the same for every day. For example, collectionist A enters at time eA and leaves at time lA every day from Sunday to Saturday.
To maximize exposure on any given day, she should arrive at the time the first collectionist enters and leave when the last to leave collectionist leaves, of course. This is impossible because she works every day and her boss won’t allow a day off. She can take several short breaks, fortunately.
Her friends told her that as soon as she enters the Manége at a time t, all the collectionists currently present will see her work. Because of this, she realizes she can take k breaks. So, she can take a break, enter at a time t and go back to work and this will guarantee her that all currently present collectionists will take a look at her work.
She’s quite an artist but unfortunately not good with numbers. Knowing the times at which each collectionist enters and leaves the exhibition hall, she wants to know the minimum number of breaks k she needs to take in order to guarantee exposure to all collectionists. Help her. Write an awesome program for her.
Note: actually, this problem is from a parallel universe so times are not the same as in ours. In this universe, the first second of a day is 0 and the last second of a day is 399,999.
Input description
The first line of input contains N (1 <= N <= 2 * 10^5), the number of collectionists attending the Manége on this day. Then follow N lines. The i-th line contains two space-separated positive integers (si,fi) (0 <= si < fi <= 399 999), the seconds at which the i-th collectionist enters and leaves, respectively.
Output description
First print k, the minimum number of breaks Irina needs to take, on a single line. Then print k lines. On the i-th line print the time at which Irina needs to show up for the i-th break.
If there are several answers, print the one with earliest break times, in order.
Sample cases
Sample Input |
Sample Output |
3 0 5 1 4 2 6 |
1 2 |
3 0 1 2 3 4 5 |
3 0 2 4 |
In the first sample case, three collectionists are present. The first collectionist arrives at second 0 and leaves at second 5. The second collectionist arrives at second 1 and leaves at second 4. The third collectionist arrives at second 2 and leaves at second 6. Irina can choose to arrive at seconds 2, 3, 4 and be guaranteed to have all three collectionists see her work. However, she chooses to arrive at second 2 because it is the earliest time.
In the second sample case, she must take 3 breaks: no more than one collectionist is present at any given time.