#K42392. Maximum Non-Overlapping Deliveries

    ID: 27077 Type: Default 1000ms 256MiB

Maximum Non-Overlapping Deliveries

Maximum Non-Overlapping Deliveries

You are given a list of flights (or deliveries), each with a start time and an end time. The objective is to determine the maximum number of non-overlapping deliveries that can be scheduled. This is a classic greedy scheduling problem which can be optimally solved by sorting the flights by their end times.

Formally, given flights with start time ( s_i ) and finish time ( f_i ), you need to select as many flights as possible such that for any two selected flights ( (s_i, f_i) ) and ( (s_j, f_j) ), the condition ( s_j \geq f_i ) holds if ( f_i ) is the finish time of the earlier flight. Use input from standard input and output the answer to standard output.

inputFormat

The first line of input contains a single integer ( n ) representing the number of flights. Each of the next ( n ) lines contains two space-separated integers representing the start time and end time of a flight.

outputFormat

Output a single integer which is the maximum number of non-overlapping deliveries that can be scheduled.## sample

3
1 4
2 3
3 5
2