#C9248. Maximum Non-overlapping Screenings
Maximum Non-overlapping Screenings
Maximum Non-overlapping Screenings
You are given a list of movie screenings, each defined by a start time and an end time. The task is to determine the maximum number of screenings you can attend without any overlaps. Two screenings are considered non-overlapping if the start time of one screening is not earlier than the end time of the previous screening.
This problem can be stated mathematically as follows: Given a set of intervals \((s_i, e_i)\) for \(i=1,2,...,n\), find the maximum number of intervals such that for any two selected intervals \((s_j, e_j)\) and \((s_k, e_k)\) with \(j \neq k\), it holds that either \(e_j \leq s_k\) or \(e_k \leq s_j\). A greedy algorithm that sorts the intervals by their end times and then selects intervals one by one is optimal for this task.
inputFormat
The input is read from stdin
and has the following format:
- The first line contains an integer \(n\) representing the number of screenings.
- The following \(n\) lines each contain two integers separated by a space, representing the start time and end time of a screening respectively.
For example:
3 1 3 2 5 4 6
outputFormat
The output is written to stdout
as a single integer on a line, representing the maximum number of non-overlapping screenings that can be attended.
For example, for the above input the output would be:
2## sample
3
1 3
2 5
4 6
2
</p>