#P3202. [HNOI2009] 通往城堡之路
[HNOI2009] 通往城堡之路
Description
It is said that the princess is imprisoned in the castle. Peng the Hero is determined: no matter how many obstacles lie ahead, no matter how powerful the guards in the castle are, no matter whether the princess might be captured again after being rescued, and no matter whether the princess is beautiful or will fall for him, he will march toward the castle without hesitation.
However, something has happened on the road to the castle. Abstractly, imagine the map lies in the first quadrant of the 2D plane. At each horizontal position there is a support point of height . If Peng the Hero fails to land on a support point, he will fall and perish on the way.
Initially, Peng the Hero is at the starting point , and the castle entrance is at . Each time, Peng the Hero can jump from support point to support point . But each jump only has energy , that is, every jump must satisfy . In other words, if the vertical difference between two adjacent support points is greater than , Peng the Hero cannot jump! Fortunately, he has a trump card.
At the starting point, he can spend one coin to increase the height of some support point by unit, or decrease it by unit. However, the heights of the support points at the starting point and at the castle entrance cannot be changed, and once he leaves the start, Peng the Hero can no longer use this trump card.
Peng the Hero is told that coins can be exchanged for one unit of life. Therefore, he wants to save as many units of life as possible by spending as few coins as possible.
He finally found you, a helpful expert. Please help him plan so that he reaches the castle while spending the fewest coins.
Input Format
The first line contains an integer (), the number of test cases. The following lines describe the input data blocks for each test case in order. Each test case occupies lines:
- The first line contains two integers and , representing the number of support points that must be traversed from the start to the castle entrance, and the maximum allowed vertical difference per jump, respectively, separated by a space. It is guaranteed that , .
- The second line contains non-negative integers separated by spaces, where () is the height of the -th support point. In particular, is the height at the start and is the height at the castle entrance. It is guaranteed that for all , .
Output Format
Output lines. The -th line () contains the minimum number of coins Peng the Hero must spend to reach the castle in the -th test case. If he cannot reach the castle no matter how he uses the trump card, output impossible. It is guaranteed that the answer fits within the int64 range.
3
10 2
4 5 10 6 6 9 4 7 9 8
3 1
6 4 0
4 2
3 0 6 3
6
impossible
4
Hint
For the first sample input block, . Lower the third support point by units, lower the sixth by unit, and raise the seventh by units. The sequence becomes: 4 5 7 6 6 8 6 7 9 8. Now the vertical difference between any adjacent support points does not exceed , so Peng the Hero can reach the castle!
For the second sample input block, . No matter how you adjust the height of the second support point, you cannot make every adjacent vertical difference at most .
For the third sample input block, . Raising the second support point by unit and lowering the third by units suffices.
Constraints
- For of the testdata, .
- For of the testdata, .
- For of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号