#K38022. Minimal Keys to Open Chests
Minimal Keys to Open Chests
Minimal Keys to Open Chests
You are given N keys, each key can open a continuous range of chests numbered with integers. Every key is represented by a pair of integers \(L\) and \(R\), which means it can open chests from \(L\) to \(R\) (inclusive). Your task is to choose a subset of these keys such that all chests from 1 to \(M\) are opened.
You need to determine the minimal number of keys required to cover the entire range \([1, M]\). If it is impossible to cover the entire range using the given keys, output IMPOSSIBLE
.
Note: Use a greedy strategy to select the key that covers the farthest reachable chest at each step.
inputFormat
The input consists of multiple lines. The first line contains two integers \(N\) and \(M\) where \(N\) is the number of keys and \(M\) is the number of chests.
Each of the following \(N\) lines contains two integers \(L\) and \(R\) indicating that the key can open all chests from \(L\) to \(R\) (inclusive).
outputFormat
Output a single line containing either the minimal number of keys required to open all chests from 1 to \(M\), or IMPOSSIBLE
if it cannot be achieved.
5 10
1 2
3 5
1 3
6 10
7 8
3