History class
Time limit10sMemory limit128 MB
Find the event order that respects disjoint time order and minimizes the largest position gap between overlapping intervals.
- Level
Hard8 of 10
- Topics
- Intervals, Topological sort, Binary search, Greedy
- Solved
- No attempts yet
Problem
Hyunsoo is a professor who teaches Korean history at Sogang University. He has historical events to cover, one per class period, so he has to decide which event belongs in which class.
Event happened during the interval . Two events are related when their intervals share at least one point. Students understand related events better when the classes covering them sit close together. Two events that are not related must be taught in the order they happened: if A and B are not related and A happened before B, then A has to be taught before B.
The distance between class and class is . Fix one order of the classes and let be the largest distance between two related events. Write a program that finds the smallest over all orders obeying the rule above. If no two events are related, is .
Input
The first line contains the number of test cases .
The first line of each test case contains the number of events (). Each of the next lines contains the two endpoints and of the interval of one event (). No two events have the same interval.
Output
For each test case, print the smallest on its own line.