Platforms at the Junction

easy intervals sorting arrays greedy 1000 ms · 256 MB

Platforms at the Junction

Riverbend Junction has n trains passing through today. Train i pulls in at minute a_i and pulls out at minute d_i, occupying a platform for every minute of the closed range [a_i, d_i] (both the arrival minute and the departure minute included).

Two trains conflict if the ranges [a_i, d_i] and [a_j, d_j] share at least one minute. In particular, if one train departs at exactly the minute another arrives, they still conflict and cannot be given the same platform — a platform is occupied through the whole of its departure minute, so there is no instant left free for a new train to pull in.

The station master wants to assign every train a platform so that no two conflicting trains ever share one. Help her find the minimum number of platforms that makes this possible.

Input

The first line contains a single integer n (1 ≤ n ≤ 2·10^5), the number of trains.

Each of the next n lines contains two integers a_i and d_i (1 ≤ a_i ≤ d_i ≤ 10^9), the arrival and departure minute of the i-th train.

Output

Print a single integer: the minimum number of platforms needed so that every train gets one and no two conflicting trains share a platform.

Notes

In the first example, trains 1 and 2 both occupy a platform during minute 10 (train 1's range is [10, 10] and train 2's range is [10, 12]), so they need separate platforms. Train 2 and train 3 overlap during minutes 11 and 12. Train 1 and train 3 never overlap. At minute 10 two platforms are in use, and at minutes 11–12 two platforms are in use again (train 2 and train 3), so 2 platforms suffice and are necessary.

In the second example the two trains' ranges [1, 3] and [5, 7] never touch, so a single platform can serve both, one after the other.

Example 1

Input

3
10 10
10 12
11 13

Output

2

Example 2

Input

2
1 3
5 7

Output

1

Hints · reveal one at a time if you're stuck

Hint 1

The minimum number of platforms equals the largest number of trains occupying the station during any single minute. Which minutes are worth checking?

Hint 2

Sort the arrival minutes and the departure minutes separately and sweep through them together. Be careful about ties: a train that arrives in the same minute another departs still needs its own platform.

report an issue Prefer your own editor? sooks prep 89 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.