#K38562. Maximum Non-Overlapping Contests

    ID: 26226 Type: Default 1000ms 256MiB

Maximum Non-Overlapping Contests

Maximum Non-Overlapping Contests

You are given several datasets. Each dataset consists of a list of contests, each with a start time and an end time. Your task is to determine the maximum number of contests that can be scheduled so that no two contests overlap in time. The optimal solution is achieved using a greedy algorithm by sorting the contests based on their finishing times and then selecting contests sequentially.

Note: Two contests are considered non-overlapping if the start time of the current contest is not less than the end time of the last selected contest.

For example, in a dataset with contests [(1, 2), (2, 3), (3, 4)], all three contests can be scheduled.

inputFormat

The input consists of multiple datasets. Each dataset is formatted as follows:

  • The first line contains an integer n representing the number of contests in the dataset.
  • The next n lines each contain two integers separated by a space, representing the start time and end time of a contest.

A line with a single 0 indicates the end of the input.

outputFormat

For each dataset, output one line containing a single integer – the maximum number of non-overlapping contests that can be scheduled.

## sample
3
1 2
2 3
3 4
4
1 5
2 6
3 4
4 5
0
3

2

</p>