Movie Marathon

MEDIUM
G
Time limit: 2.0sMemory limit: 256MB
greedyintervals

Movie Marathon

You're given n movie screenings, each with a start time and an end time (a screening ​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​‌‌​​‌​‌​​‌‌​‌‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​​​​​‌‌​‌‌‌​‌‌​​​‌‌​​‌‌​​‌​​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​‌‌​​‌‌‌​​‌​​‌‌‌​​​​​‌‌​​‌‌occupies the half-open interval [start, end), so a screening ending exactly when another starts does not conflict with it). You can only watch one screening at a time. Find the maximum number of screenings you can watch in full.

Input

​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​‌‌​​‌​‌​​‌‌​‌‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​​​​​‌‌​‌‌‌​‌‌​​​‌‌​​‌‌​​‌​​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​‌‌​​‌‌‌​​‌​​‌‌‌​​​​​‌‌​​‌‌Line 1: integer n (1 ≤ n ≤ 2·10^5) Next n lines: two integers start end (0 ≤ start < end ≤ 10^9)

Output

A single integer: the maximum number of non-overlapping screenings.

Example

Input:

3
1 5
2 3
4 6

Output:

2
​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​‌‌​​‌​‌​​‌‌​‌‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​​​​​‌‌​‌‌‌​‌‌​​​‌‌​​‌‌​​‌​​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​‌‌​​‌‌‌​​‌​​‌‌‌​​​​​‌‌​​‌‌```
Explanation: watch `[2,3)` then `[4,6)`.

### Constraints
- 1 ≤ n ≤ 2·10^5
- 0 ≤ start < end ≤ 10^9
​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​‌‌​​‌​‌​​‌‌​‌‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​​​​​‌‌​‌‌‌​‌‌​​​‌‌​​‌‌​​‌​​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​‌‌​​‌‌‌​​‌​​‌‌‌​​​​​‌‌​​‌‌

Sample Testcases

Case 1

Input
3
1 5
2 3
4 6
Expected Output
2

Case 2

Input
3
1 2
2 3
3 4
Expected Output
3
Loading Cloud IDE...
Ln 1, Col 1Spaces: 4UTF-8
pythonAuto-Saved

Input

3
1 5
2 3
4 6

Expected Output

2

Run or submit your code to see output here.