PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#GD0000083. Các trạm xăng (Gas Stations)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Các trạm xăng (Gas Stations)
Gas Stations
Source: UVa
Version: Phuoc Hung OJ Extended
Problem Statement
Along a road of length , there are gas stations. Station at serves the closed interval . Close as many stations as possible while ensuring every point in remains served by at least one open station. If full road coverage is impossible even with all stations open, print -1.
Input
The first line contains . Each of the next lines contains integers .
Output
Print the maximum number of stations that can be closed, or -1 if covering the whole road is impossible.
Subtasks
- Subtask 1 (20 points): ; ; ; .
- Subtask 2 (30 points): ; ; ; .
- Subtask 3 (50 points): ; ; ; .
Examples
Input
40 5
5 5
11 8
20 10
30 10
40 10
Output
2
Explanation
The stations serve , , , , and . Keep stations , , and to cover the road using three. Two stations cannot cover both endpoints without a gap, so two of the five can be closed.