Ladders

5

3 votes
Dynamic Programming, Algorithms, Medium, Introduction to Dynamic Programming 1
Problem

You are visiting a building of N floors. On every floor, only one ladder of specified length is present. If the length of the ladder is x units, you can reach x floors above from the current one. Refer to the example below for clear understanding.

For example, if a person is on the 2nd floor where a ladder of 3 units is present, he/she can reach any of 3rd/4th/5th floor as per his/her choice.

 

You can leave the ladder in between in order to change the ladder, but you can only start from the starting floor of the ladder.

For example, if a person is on the 2nd floor where a ladder of 3 units is present (let's denote ladder by L1), and there is a ladder on 3rd floor of 3 units say L2, and a person starts from 2nd floor with ladder L1, and reaches 4th floor, he cannot use the ladder L2 from 4th floor or any floor except the 3rd floor. Since the ladder L2 is present on 3rd floor, he can only start using it from that floor.


You are given Q questions. In each question, you will be given a floor number. For each question, you have to tell the least number of ladders required to reach given floor.
Initially, you are on the ground floor.

Input Format:
The first line contains an integer T , indicating the number of test cases.
For each test case:
The first line contains an integer N, indicating number of floors in the building.
Next line contains N space separated positive integers which denote the length of ladder at each floor (First integer corresponds to ladder length on ground floor, second integer corresponds to ladder length on first floor and so on) .
Next line contains an integer Q, indicating number of questions.
Following Q lines contain an integer each, denoting the floor number for which answer is to be computed.

Output Format:
For each question, print the least number of ladders required required to reach given floor.
Answer for each question should come in a new line.

Input Constraints
1T10
1N,Q105
1ladder lengthN
1query valueN

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

enter image description here
We can reach 1st floor using only 1 ladder.
2nd floor can be reached using 2 ladders.
3rd floor can be reached using 2 ladders.
4th floor can be reached using 3 ladders.
5th floor can be reached using 3 ladders.

Editor Image

?