#C3314. Maximum Non-Overlapping Events

    ID: 46728 Type: Default 1000ms 256MiB

Maximum Non-Overlapping Events

Maximum Non-Overlapping Events

You are given a set of events, each defined by its start time and end time. Your task is to select the maximum number of non-overlapping events that can be scheduled. Two events are considered non-overlapping if the start time of an event is not less than the end time of the previous event.

This is a typical greedy scheduling problem where sorting the events by their end times yields an optimal solution.

The problem can be formally described as follows:

Given \( n \) events with start and end times \( (s_i, e_i) \), find the maximum number \( k \) such that you can select events \( i_1, i_2, \ldots, i_k \) with \( s_{i_1} \le e_{i_1} \) and for each \( 1 \leq j < k \), \( s_{i_{j+1}} \geq e_{i_j} \). Use the greedy approach of choosing the event with the smallest end time.

inputFormat

The input is given via standard input (stdin) and has the following format:

n
start_1 end_1
start_2 end_2
... 
start_n end_n

Where:

  • \( n \) is an integer that represents the number of events.
  • Each of the next \( n \) lines contains two integers representing the start and end times of an event.

outputFormat

Output a single integer to standard output (stdout): the maximum number of non-overlapping events that can be scheduled.

## sample
5
1 3
2 5
4 6
6 8
9 10
4