Cover Line

4.3

6 votes
Problem

Alice was given a number line containing every positive integer, x, where 1<=x<=n. She was also given m line segments, each having a left endpoint, l (1<=l<=n), a right endpoint, r (1<=r<=n), and an integer weight, w.

Her task is to cover all numbers on the given number line using a subset of the line segments, such that the maximum weight among all the line segments in the chosen subset is minimal.

In other words, let S=(l1,r1,w1),(l2,r2,w2),....,(lk,rk,wk), represent a set of k line segments chosen by Alice, max(w1,w2,...,wk) should be minimized. 

All numbers 1,2,....n should be covered by at least one of k the chosen line segments. It is okay for the chosen line segments to overlap. 

You program should output the minimized maximum weight in the chosen subset that covers all the numbers on the number line, or -1 if it is not possible to cover the number line. 

Input format

  • The first line of the input contains an integer, n - denoting the range of numbers on the number line, [1,n].
  • The second line of the input contains an integer, m - denoting the number of line segments. 
  • The ith line among the next m lines each contain 3 integers each, li,  riwi (li<=ri) - denoting the end points, and weight of the ith line segment. The line segments may overlap.

Output format

The output should contain one integer, denoting the minimized maximum weight among all the chosen k segments, that completely cover the number line, or -1 if it is not possible to cover the number line

 Constraints

1n1091m1051lirin1wi109

 

Sample Input
5 
3 
1 2 2 
3 5 2
1 5 10
Sample Output
2
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

In the sample case n=5, so we want to cover all numbers from [1,2,..,5]. The third line segment covers all the numbers, but with a weight of 10, since we are trying to minimize the maximum weight, it makes more sense to use the first and second line segment to cover the number line, with a maximum weight of 2.

Editor Image

?