I have been stuck on a problem in the PEG Online Judge, called Dominos, which you can find here:
http://wcipeg.com/problem/domino
Abridged Description:
We are given a list of dominos with different heights and locations, arranged horizontally. A domino at position x with height h, once pushed to the right, will knock all dominoes at positions x+1, x+2, ..., x+h rightward as well. Conversely, the same domino pushed to the left will knock all dominoes at positions x-1, x-2, ..., x-h leftward.
What is the minimum number of pushes we can do to topple all dominos?
Example:
|
| |
| | | | | |
1 2 3 4 5 6 7 8
Answer is 2. Push the domino at location 1 rightward, the domino at location 8 leftward.
Constraints:
The input starts with a single integer N ≤ 100,000, the number of dominoes, followed by N pairs of integers. Each pair of integers represents the location and height of a domino. (1 ≤ location ≤ 1,000,000,000, 1 ≤ height ≤ 1,000,000,000) No two dominoes will be in the same location.
Memory Limit: 64mb
Time Limit: 1.00s
NOTE: 60% of test data has N ≤ 5000.
There is a brute-force solution which solves the problem only for 60% of the input.
It looks like there should be a subquadratic, or even linear solution using dynamic programming in order to get AC for the largest input size.
Any hints would be appreciated.
There is a hint from the author, which I could not quite understand, in case it is helpful:
Make a recursive function f(n) that gives the minimum # of moves required to topple the first n dominoes.
Now how do you relate f(n) to previous values of f? Domino #n has two choices: go left (in which case it topples other dominoes over) or go right (in which case another domino to its left topples it over). Try working from there.
Thanks!