I am working on the following task:
You are given a sorted collection of springs. Each of them has been produced in certain dimensions, and each stretching, or compression, requires quite a bit of effort.
It takes 1 unit of effort to lengthen or shorten a spring that has not yet been moved by a centimeter. Each successive stretch, or compression, of the same spring requires an additional 1 unit of effort. More precisely, if you want to lengthen a spring by D centimeters, he will exert himself by 1,2,…,D successive units of effort.
Your task is to answer q queries about how much minimal effort it would cost to set the k-shortest springs to the same length modulo 109+7.
Constraints: N,Q <= 106, xi <= 109
Input:
N, Q
x1, x2, x3, …
q1, q2, q3, …
N – number of springs
xi – length of i-th spring (Note that xi <= xi+1)
qi – how many springs should be set to the same length in i-th question
Output:
Answers to questions – minimal effort it would cost to set the k-shortest springs to the same length modulo 10^9+7.
My solution attempt:
I discovered that the minimal effort would be achieved when the springs are set to the average length of the springs. However in this approach complexity of algorithm is O(n*q) which is too slow for given constraints.
How can I solve this faster?
In my brutal approach I calculate the average length of the springs and then linearly calculate the cost for every spring. It is too slow for N,Q <= 106 but is enough for n,q <= 103.
randomAlgo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.