Farmer John Actually Farms
Time limit2sMemory limit1024 MB
Find the minimum day D so that each plant's height h_i + a_i*D has exactly t_i taller plants, or report -1 if no such day exists.
- Level
Hard8 of 10
- Topics
- Binary search, Sorting, Greedy
- Solved
- No attempts yet
Problem
Farmer John is growing () asparagus plants on his farm. Some plants have genetic differences, so some grow faster than others. The initial height of the th plant is inches, and each day the th plant grows by inches.
FJ likes some of his plants more than others, so he wants certain plants to be taller than others. He gives you an array of distinct values that contains every integer from to . He wants the th plant to have exactly other plants that are taller than it. Find the minimum number of days needed to satisfy his request, or determine that it is impossible.
Input
The first line contains an integer (), the number of test cases.
Each test case starts with a line containing an integer .
The second line contains integers (), the initial height of the th plant in inches.
The third line contains integers (), the number of inches the th plant grows each day.
The fourth line contains distinct integers .
The sum of over all test cases does not exceed .
Output
Output lines, one per test case, each containing the answer. Output if the request is impossible.
The values can be large, so use a 64-bit integer type such as long long in C/C++.