#K92347. Maximum Non-overlapping Gaming Sessions

    ID: 38177 Type: Default 1000ms 256MiB

Maximum Non-overlapping Gaming Sessions

Maximum Non-overlapping Gaming Sessions

You are given n gaming sessions, each specified by a start time and an end time. Two sessions are considered overlapping if they share any common time interval. Your task is to compute the maximum number of gaming sessions that can be attended without any overlap.

Use a greedy algorithm by sorting the sessions by their end times and iteratively selecting sessions that start after or exactly when the last selected session ended.

Note: The input is read from standard input and the output should be written to standard output. All times are given in minutes.

Mathematical Formulation:

Let \( S = \{(s_1, e_1), (s_2, e_2), \ldots, (s_n, e_n)\} \) be the set of sessions. We wish to find the largest subset \( S' \subseteq S \) such that for any two sessions \( (s_i, e_i), (s_j, e_j) \in S' \) with \( i \neq j \), either \( e_i \leq s_j \) or \( e_j \leq s_i \).

inputFormat

The first line contains an integer n representing the number of gaming sessions. The following n lines each contain two integers start and end, representing the start and end times of a session. It is guaranteed that 0 ≤ start < end ≤ 1440.

Input Format:

n
start1 end1
start2 end2
... 
startn endn

outputFormat

Output a single integer representing the maximum number of non-overlapping gaming sessions that can be attended.

Output Format:

result
## sample
3
540 600
630 690
120 180
3