If I Could Turn Back Time

시간 제한2초메모리 제한2048 MB

요약
문턱값 침식이 산 높이 p를 h로 바꾸는 데 필요한 최소 연수를 구하고, 불가능하면 -1을 출력한다.
난이도

보통10점 중 6점

유형
그리디, 정렬, 구현, 수학
정답자
아직 제출이 없습니다

문제

Inna is an avid hiker. She's visiting a range of nn mountains with heights h_1,h_2,…,h_nh\_1, h\_2, \ldots, h\_n.

At a nearby shop, Inna has found a book that mentions that at some point in the past, the heights of the mountains were p_1,p_2,…,p_np\_1, p\_2, \ldots, p\_n in the same order. However, there is no evidence of how old this book is.

The book also describes a model of erosion that makes the mountains shorter year after year. Every year, based on the weather, a certain height threshold xx can be determined. Then, every mountain with the current height of at least xx decreases in height by exactly 11. Different years can have different values of xx.

Inna is curious how old the book actually is, and whether the described model is sound. Help her figure out the smallest number of years in which erosion could take the mountains from heights p_1,p_2,…,p_np\_1, p\_2, \ldots, p\_n to heights h_1,h_2,…,h_nh\_1, h\_2, \ldots, h\_n in the same order, or determine that it is impossible under the given model.

입력

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn, denoting the number of mountains (1≤n≤1051 \le n \le 10^5).

The second line contains nn integers h_1,h_2,…,h_nh\_1, h\_2, \ldots, h\_n, denoting the current heights of the mountains (1≤h_i≤1061 \le h\_i \le 10^6).

The third line contains nn integers p_1,p_2,…,p_np\_1, p\_2, \ldots, p\_n, denoting the heights of the mountains in the same order at some point in the past (1≤p_i≤1061 \le p\_i \le 10^6).

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

출력

For each test case, print the smallest number of years in which erosion could take the mountains from heights p_1,p_2,…,p_np\_1, p\_2, \ldots, p\_n to heights h_1,h_2,…,h_nh\_1, h\_2, \ldots, h\_n, or a single integer −1-1 if the described model is unsound.

힌트

In the first test case, the heights of the mountains could go from (5,3,6,2)(5, 3, 6, 2) to (3,2,4,2)(3, 2, 4, 2) in just two years:

  • Suppose that in the first year, x=4x = 4. After this year, the heights of the mountains are (4,3,5,2)(4, 3, 5, 2).
  • Suppose that in the second year, x=3x = 3. After this year, the heights of the mountains are (3,2,4,2)(3, 2, 4, 2).

예제1

  1. 예제 1

    입력
    4
    4
    3 2 4 2
    5 3 6 2
    1
    10
    100000
    5
    1 2 3 4 5
    1 2 3 4 5
    3
    1 4 6
    4 1 8
    
    예상 출력
    2
    99990
    0
    -1