luogu#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!

题目大意

给定 nn 个区间,第 ii 个区间为 [ai,bi][a_i,b_i]。你需要把这些区间按某种顺序排列,使得两个区间如果没有交点,则左端点更小的区间需要排在前面。

设第 ii 个区间排在第 pip_i 个位置,定义区间 iijj 之间的距离为 pipj|p_i-p_j|。令 kk 为任意两个相交的区间之间的最大距离,你需要最小化 kk,并输出一组对应的合法顺序。

对于 100%100\% 的数据,满足 1n5×1041\leq n\leq 5\times 10^40ai,bi1090\leq|a_i|,|b_i|\leq10^9

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。