DOMINOES - Dominoes

no tags 

Johnny is playing with some dominoes one afternoon. His dominoes come in a variety of heights and colors.

Just like any other child, he likes to put them in a row and knock them over.
He wants to know something: how many pushes does it take to knock down all the dominoes?
Johnny is lazy, so he wants to minimize the number of pushes he takes.
A domino, once knocked over, will knock over any domino that it touches on the way down.

For the sake of simplicity, imagine the floor as a one-dimensional line, where 1 is the leftmost point. Dominoes will not slip along the floor once toppled. Also, dominoes do have some width: a domino of length 1 at position 1 can knock over a domino at position 2.
For the mathematically minded:
A domino at position x with height h, once knocked over to the right, will knock all dominoes at positions x+1, x+2, ..., x+h rightward as well.
Similarly, the same domino knocked over to the left will knock all dominoes at positions x-1, x-2, ..., x-h leftward.


The input starts with a single integer N (N ≤ 100000), the number of dominoes, followed by N pairs of integers.
Each pair of integers represents the location and height of a domino, in that order (0 ≤ location ≤ 109, 0 ≤ height ≤ 109).
No two dominoes will have the same location.


A single integer on a single line: the minimum number of pushes Johnny must make in order to ensure that all dominoes are knocked over.


1 1
2 2
3 1
5 1
6 1
8 3
  |           |
| | |   | |   |
1 2 3 4 5 6 7 8

Pushing 1 causes 2 and 3 to fall, while pushing 8 causes 6 to fall and gently makes 5 tip over as well.

hide comments
Aditya Muraletharan: 2013-01-17 18:24:50

Excellent problem:)
Do not make any assumptions about the order of dominoes. Cost me a lot of WA's.

rahul singal: 2011-05-31 13:12:06

what does h==0 mean ?? should a domino be pushed at this position ?

Brian Bi: 2009-12-13 18:53:39

hmm, you are right, sorry about that (*sigh* again it's Hanson's test data but I really should've checked it before uploading it)

Abir: 2009-12-13 18:53:39

I sent a checker and found that input data contains height = 0.

Added by:Brian Bi
Time limit:0.104s-1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 NODEJS PERL6 VB.NET
Resource:Hanson Wang