Smoothing Window (Small)
Time limit5sMemory limit512 MB
Given sliding window sums of an unknown integer sequence, find the smallest possible range between its largest and smallest values.
- Level
Medium6 of 10
- Topics
- Binary search, Intervals, Prefix sum
- Solved
- No attempts yet
Problem
Adamma is a climate scientist who studies temperature. Every minute she records the current temperature as an integer, which gives a long list of integers . Adamma uses her own temperature scale instead of a familiar one such as Celsius or Kelvin, so the values can be large and negative. She often plots these temperatures on her screen.
This morning she computed a sliding average of the list to get a smoother plot. She used a smoothing window of size , which turns the sequence of temperatures into a sequence of average temperatures . Each is the average of . The original were all integers, but some may be fractional.
Adamma forgot to save the original sequence of temperatures. Now she wants a different quantity: the difference between the largest and the smallest temperature, . All she still has is , , and the smoothed sequence.
Several original sequences may produce the same smoothed sequence, so this difference is not always determined. In that case Adamma wants the smallest difference over every integer sequence that produces her smoothed sequence with the given and .
Input
The first line contains the number of test cases . Each test case takes two lines. The first line contains the integers and separated by a space. The second line contains the integers separated by spaces, where .
Limits
Output
For each test case, print one line of the form Case #x: y, where x is the test case number starting from 1 and y is the smallest possible difference between the largest and the smallest temperature.
Hint
In the first case of the example the smoothed sequence is . The integer sequence that gives the smallest difference is . The sequence produces the same smoothed sequence with a difference of 4, but it is not a valid answer because the original temperatures are known to be integers.
In the second case the only known fact is that the sum of the 100 original values is . All of them may be exactly , and then the difference is 0, which is as small as a difference gets.
In the third case the original sequence could have been .