LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Basics
Data types and IOC++ syntaxModuloVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search Functions
Binary Numbers
Binary NumbersNumbers in codeBitwise Operations
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of Eratosthenes
Data Structures
StringsStackQueue
Dynamic Programming
About DPDP problems

Movie Marathon

EasyProblem #9
Time LimitMemoryInputOutput
1 s64 MBstdinstdout

Think about which movie is the safest one to watch first.

A TV channel is airing a movie marathon tonight. The schedule lists n movies, and for each movie the time it starts and the time it ends. You want to watch as many movies as possible, but only whole movies - no skipping parts, no watching two at once.

The moment one movie ends, you are free to start another one (a movie that starts exactly when the previous one ends is fine).

Formally, given n intervals, find the largest number of them that can be chosen so that no two chosen intervals overlap.

Input

First line of input will be a single number n, the number of movies.
In the next n lines will be two numbers ai​ and bi​, the start and end time of each movie.

Output

A single number - the largest number of whole movies you can watch.

Example

Input
4
1 3
2 5
4 7
6 9
Output
2

You watch the movie 1→3, skip 2→5 (it already started while you were watching), watch 4→7, and skip 6→9. Two movies.

Constraints

1≤n≤2⋅105
0≤ai​<bi​≤109

Submit your solution

Sign in to submit your solution and track your progress.

Sign in to submit