#P7013. [CERC2013] History course

[CERC2013] 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 [ai,bi].[a_i, 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 BB , then the lecture on A should precede the lecture on B)B) .

Find the minimum integer k0k \ge 0 and an order of the lectures such that any two related events are scheduled at most kk lectures apart from each other (lectures number ii and jj are considered to be ij|i−j| lectures apart).

输入格式

The first line of input contains the number of test cases tt . The descriptions of the test cases follow:

The first line of each test case contains the number n(1n50000)n (1 \le n \le 50000) . Each of the next nn lines contains two integers aia_i and bi(109aibi109)b_i (-10^9 \le a_i \le b_i \le 10^9) - the ends of the i-th interval. The intervals are pairwise different.

输出格式

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 kk . The next nn 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 kk lectures apart. Remember to put any unrelated intervals in the proper order!

1
3
1 6
2 3
4 5

1
2 3
1 6
4 5

提示

Time limit: 10 s, Memory limit: 128 MB.

感谢 hht2006 提供的 Special Judge。