Snow Boots
Time limit2sMemory limit512 MB
Find the minimum number of boot pairs Farmer John must discard, given a stack-ordered backpack and snow-depth and step-size limits, to walk from tile 1 to tile N.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Array, Brute force, Implementation
- Solved
- No attempts yet
Problem
Winter has arrived on the farm, and with it the snow. A path of tiles runs from the farmhouse to the barn, numbered through , and tile is covered by feet of snow.
Farmer John starts on tile and has to reach tile to wake the cows. Tile sits under the farmhouse roof and tile sits under the barn roof, so neither one holds any snow. To step on any other tile, Farmer John needs boots.
His foul-weather backpack holds pairs of boots, numbered through . Some pairs are sturdier and some are nimbler: pair lets him step in snow at most feet deep, and lets him move at most tiles forward in one step.
The boots are packed in a stack, so only the topmost pair is within reach. At any moment Farmer John can put on the topmost pair, which discards the pair he was wearing, or discard the topmost pair without wearing it, which exposes the pair below.
Farmer John changes boots only while standing on a tile. If that tile holds feet of snow, the pair he takes off and the pair he puts on must both withstand at least feet. A pair he discards without ever wearing it is free of this restriction.
Farmer John starts out wearing no boots. Find the smallest number of pairs he has to discard to reach the barn.
Input
The first line contains two integers and separated by a space ().
The second line contains integers separated by spaces. The th of them is , the depth of snow on tile (). It is guaranteed that .
Each of the next lines contains two integers separated by a space. Line holds , the greatest snow depth pair can step in, and , the greatest step size for pair (, ).
The pairs are listed from the top of the backpack downward, so pair is the topmost pair.
Output
Print one integer, the smallest number of pairs Farmer John has to discard. Reaching the barn is always possible.