#C10914. Maximum Non-overlapping Events

    ID: 40172 Type: Default 1000ms 256MiB

Maximum Non-overlapping Events

Maximum Non-overlapping Events

You are given a list of events, each represented by a pair of integers [start, end] indicating the start and end times of the event. Your task is to determine the maximum number of events you can attend without any overlapping. Two events do not overlap if the start time of one event is greater than or equal to the end time of the other.

Hint: A greedy strategy that always chooses the event that finishes first is optimal. The formula for the decision process can be summarized as:

\( \text{if } start \geq last\_end\_time \text{ then select event} \)

inputFormat

The input is given via standard input (stdin) and formatted as follows:

  • The first line contains a single integer \(n\), representing the number of events.
  • The following \(n\) lines each contain two space-separated integers, representing the start time and end time of an event respectively.

Example:

3
1 3
2 4
3 5

outputFormat

Output to standard output (stdout) a single integer, which is the maximum number of non-overlapping events that can be attended.

Example:

2
## sample
3
1 3
2 4
3 5
2