#C7495. Maximum Non-overlapping Events

    ID: 51372 Type: Default 1000ms 256MiB

Maximum Non-overlapping Events

Maximum Non-overlapping Events

You are given a list of events, where each event is represented as a pair of integers \( (s, e) \) indicating its start time \( s \) and end time \( e \). Two events are non-overlapping if the start time of an event is not less than the end time of the previous event, i.e., if event \( A \) ends at \( e_A \), then event \( B \) can be scheduled if \( s_B \ge e_A \).

Your task is to determine the maximum number of non-overlapping events that can be scheduled. The optimal strategy is to use a greedy algorithm by first sorting the events by their end times and then selecting events sequentially.

Note: The events are assumed to be given in an arbitrary order. Use the following condition to choose events: if the current event's start time \( s \) is greater than or equal to the last selected event's end time \( e_{last} \), then this event can be selected.

inputFormat

The input is read from standard input and has the following format:

  • The first line contains a single integer \( n \) representing the number of events.
  • Each of the next \( n \) lines contains two space-separated integers \( s \) and \( e \), representing the start and end times of an event.

outputFormat

Output a single integer to standard output, which denotes the maximum number of non-overlapping events that can be scheduled.

## sample
1
1 3
1