Given N, K and every window sum of a hidden integer sequence, find the smallest max-minus-min range among all integer sequences that match.
Medium7Binary searchMathPrefix sumNo attempts yetTime limit5sMemory limit512 MBAdamma is a climate scientist who studies temperature. Every minute she writes down the current temperature as an integer, which builds a long list of integers x1,x2,…,xN. She uses her own temperature scale instead of a familiar one such as Celsius or Kelvin, so the values can be large and they can be negative. She often plots these temperatures on her computer screen.
This morning she computed a sliding average of the list to get a smoother plot. She used a smoothing window of size K, which turns the sequence of N temperatures into a sequence of N−K+1 average temperatures s1,s2,…,sN−K+1. Each si is the average of xi,xi+1,…,xi+K−1. The original values xi are all integers, but some si can be fractional.
Adamma forgot to save the original sequence of temperatures. Now she wants a different number: the difference between the largest temperature and the smallest temperature, that is max{x1,…,xN}−min{x1,…,xN}. All she has left is N, K, and the smoothed sequence.
Several original sequences can produce the same smoothed sequence, so that difference is not determined. Adamma therefore asks for the smallest difference over every integer sequence that matches the given N, K, and smoothed values.
The first line contains the number of test cases T. Then T test cases follow, and each one takes two lines. The first line contains the integers N and K separated by a space. The second line contains the integers sum1,sum2,…,sumN−K+1 separated by spaces, where si equals sumi/K.
For each test case, print one line in 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.
In the first sample case the smoothed sequence is 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5. The integer sequence that gives the smallest difference is 0, 1, 1, 2, 2, 3, 3, 4, 4, 5. The sequence 0.5, 0.5, 1.5, 1.5, 2.5, 2.5, 3.5, 3.5, 4.5, 4.5 produces the same smoothed sequence with a difference of 4, but its entries are not integers, so it is not a valid original sequence.
In the second sample case the only known fact is that the 100 original values add up to -100. Every value can be exactly -1, and then the difference is 0. No difference is smaller than that.
In the third sample case the original sequence can be -4, 8, -4, 8, -4, 8, -4.