Black Holes and Asteroids
InterviewTime limit1sMemory limit1024 MB
Given black hole and asteroid positions, find the smallest integer P so that every asteroid is within P/w of some black hole.
- Level
Medium4 of 10
- Topics
- Sorting, Binary search, Greedy
- Solved
- No attempts yet
Problem
There are black holes and asteroids on a number line in the current simulation universe. All black holes have the same pulling force .
If black hole is at position , and asteroid is at position with mass , then black hole pulls in asteroid when .
One black hole can pull in several asteroids. When several different black holes can pull in the same asteroid, the leftmost black hole pulls it in.
Find the minimum integer that lets the black holes pull in every asteroid in the simulation universe.
Input
The first line contains the number of black holes and the number of asteroids , separated by a space.
The second line contains integers , separated by spaces.
From the third line onward, the next lines describe the asteroids. The -th of these lines contains two integers and , separated by a space.
Each position holds either a single black hole or a single asteroid.
Output
Print the minimum integer that lets the black holes pull in every asteroid.
Hint
is the absolute value of . If , then , and if , then .