All Tracks Algorithms Dynamic Programming State space reduction Problem

Sonya and sticks
Tag(s):

Algorithms, Dynamic Programming, Medium-Hard

Problem
Editorial
Analytics

Pussycat Sonya placed $$N$$ sticks on a straight line. The $$i$$-th stick is located at position $$x_i$$ and has height $$h_i$$. There are no two sticks located at the same position. Sonya can push sticks to make them fall to the left or the right side. While falling, a stick can touch its neighbors and make them also fall in the same direction (like dominoes).

More formally: if a stick at position $$x$$ with height $$h$$ is falling to the $$\mathit{left}$$, it will make all the sticks at positions $$(x - h) \dots x$$ inclusive fall to the left. Similarly, if a stick is falling to the $$\mathit{right}$$ it will make all the sticks at positions $$x...(x + h)$$ inclusive fall to the right.

Now Sonya is interested in finding the minimal number of sticks she needs to push to make all $$N$$ sticks fall.

Input format:
The first line contains one integer $$N$$ $$-$$ the number of sticks.
Each of the next $$N$$ lines contains two integers $$x_i, h_i$$ $$-$$ position and height of the $$i$$-th stick.

Output format:
Print the minimal number of sticks Sonya needs to push.

Constraints
$$1 \leq N \leq 5 \cdot 10^5$$
$$1 \leq x_i, h_i \leq 2 \cdot 10^9$$

15 points
$$1 \leq N \leq 10$$

10 points
$$1 \leq N \leq 300$$

10 points
$$1 \leq N \leq 5000$$

65 points
Original constraints

SAMPLE INPUT
5
1 2
3 1
6 2
7 1
9 2
SAMPLE OUTPUT
2
Explanation

The optimal solution is to push the stick at position $$1$$ to the right. It will also make the stick at position $$3$$ fall.
And then push stick at position $$9$$ to the left. It will make the stick at position $$7$$ fall to the left, which will cause the stick at position $$6$$ to also fall to the left.

Time Limit: 2.0 sec(s) for each input file.
Memory Limit: 512 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Visual Basic

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

This Problem was Asked in

HackerEarth

Challenge Name

February Clash '17

OTHER PROBLEMS OF THIS CHALLENGE
Notifications
View All Notifications