eolymp
bolt
Try our new interface for solving problems
Problems

History course

History course

You are to give a series of lectures on important historical events, one event per lecture, in some order. Each event lasted for some time interval \[\textbf{a_i}, \textbf{b_i}\]. We say that two events are related if their intervals have a common point. It would be convenient to schedule lectures on related events close to each other. Moreover, lectures on unrelated events should be given in the order in which the events have taken place (if an event A preceded an unrelated event \textbf{B}, then the lecture on \textbf{A} should precede the lecture on \textbf{B}). Find the minimum integer \textbf{k} ≥ \textbf{0} and an order of the lectures such that any two related events are scheduled at most \textbf{k} lectures apart from each other (lectures number \textbf{i} and \textbf{j} are considered to be \textbf{|i−j|} lectures apart). \InputFile The first line of input contains the number of test cases \textbf{t}. The descriptions of the test cases follow: The first line of each test case contains the number \textbf{n }(\textbf{1 }≤ \textbf{n }≤ \textbf{50000}). Each of the next \textbf{n }lines contains two integers \textbf{a_i} and \textbf{b_i} (\textbf{-10^9} ≤ \textbf{a_i} ≤ \textbf{b_i} ≤ \textbf{10^9}) - the ends of the \textbf{i}-th interval. The intervals are pairwise different. \OutputFile Print the answers to the test cases in the order in which they appear in the input. The first line of the answer to each test case should contain the minimum value of \textbf{k}. The next \textbf{n }lines should list the intervals (in the same format as in the input) in an order such that any two related events are scheduled at most \textbf{k }lectures apart. Remember to put any unrelated intervals in the proper order!
Time limit 30 seconds
Memory limit 256 MiB
Input example #1
1
3
1 6
2 3
4 5
Output example #1
1
Source 2013 ACM ICPC Central Europe Regional Contest, Kraków, November 15-17, Problem G