If I Could Turn Back Time
Time limit2sMemory limit2048 MB
Given current heights h and an earlier record p, find the fewest erosion years that turn p into h, or -1 if the model cannot do it.
- Level
Medium6 of 10
- Topics
- Array, Implementation
- Solved
- No attempts yet
Problem
Inna is an avid hiker. She is visiting a range of mountains with heights .
At a nearby shop, Inna found a book stating that at some point in the past, the mountains had heights , in the same order. The book does not say how old it is.
The book also describes an erosion model that makes the mountains shorter year after year. Each year, the weather sets a height threshold . Every mountain whose current height is at least decreases by exactly . Different years can use different values of .
Inna wants to know how old the book is and whether the model is sound. Find the smallest number of years in which erosion could take the mountains from heights to heights , in the same order, or determine that the model cannot do this.
Input
The first line contains the number of test cases (). Each test case follows.
The first line of a test case contains (), the number of mountains.
The second line contains integers (), the current heights.
The third line contains integers (), the heights in the same order at some point in the past.
The sum of over all test cases does not exceed .
Output
For each test case, print the smallest number of years in which erosion could take the mountains from to . If the model is unsound, print instead.
Hint
In the first test case, the heights can go from to in just two years.
- In the first year, let . Afterward, the heights are .
- In the second year, let . Afterward, the heights are .