Toppling Dominoes
Limits: 2 sec., 512 MiB
Zenyk and Marichka set up dominoes in a row and want to knock all of them down, pushing as few of them by hand as possible.
There are \(n\) dominoes in the row. The \(i\)-th domino stands at position \(x_i\) and has height \(h_i\). The dominoes are numbered in increasing order of their positions: \(x_1 < x_2 < \ldots < x_n\). The dominoes are considered to be flat.
Dominoes can only fall to the right. When the \(i\)-th domino falls, it pushes every domino in the interval \([x_i, x_i + h_i)\), that is, it hits a domino at position \(x\) if \(x_i \le x < x_i + h_i\). Every domino that is hit also falls to the right and may knock down others.
Find the smallest number of dominoes that have to be pushed by hand so that in the end all the dominoes fall.
Input
The first line contains one integer \(n\) – the number of dominoes.
Each of the next \(n\) lines contains two integers \(x_i\) and \(h_i\) – the position and the height of the \(i\)-th domino.
The dominoes are given in increasing order of their positions: \(x_1 < x_2 < \ldots < x_n\).
Output
Print one integer – the smallest number of dominoes that have to be pushed by hand.
Constraints
\(1 \le n \le 2 \cdot 10^5\),
\(1 \le x_i, h_i \le 10^9\),
\(x_1 < x_2 < \ldots < x_n\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 5 1 3 3 5 7 4 10 1 20 1 | 2 |
| Input (stdin) | Output (stdout) |
|---|---|
| 2 1 2 3 1 | 2 |
| Input (stdin) | Output (stdout) |
|---|---|
| 1 1000000000 1000000000 | 1 |
Notes
In the first example, it is enough to push the dominoes at positions 1 and 20 by hand. The first of them knocks down the domino at position 3, which knocks down the one at position 7, which knocks down the one at position 10.
In the second example, the domino at position 1 does not reach the domino at position 3, because the right end of the interval \([1, 3)\) is not included. So both dominoes have to be pushed by hand.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|