-2

You have an array of size n of positive integers on which you can do the following operation by at most N times:

  • Choose a sub-array and decrease its elements value by k (k must be smaller than the sub-array's minimum value).
  • Such an operation costs the size of the sub-array multiplied by k.
  • The total costs of these operations must not be larger than M.
  • N and M can be extremely large.

Can you give me an efficient algorithm for minimizing the maximum element in this array?

rrufai
  • 1,475
  • 14
  • 26
GEP
  • 311
  • 3
  • 12

3 Answers3

1

If we can make a function that can check if a given maximum can be achieved, then we can find the solution using binary search on the maximum. (each time the maximum is achievable, decrease it, if it's not achievable, increase it).

the checking function might look like this:

def check(array, M, i, j, max)

this will return true if it's possible to reduce everything between i and j to below max with M cost. We start of with i=0, j=len(array), max=max_guess

you now have many options of what to do inside check. you might try:

possible = check(M/2, i..i+j/2, ..) and check(M/2, i..i+j/2,..)
return possible

however splitting M into both halves isn't a good idea because one half may require more. Perhaps you could use binary search to determine how to split M into the halves. However maybe it's not even possible to split the array into half because some subsections might go over both halves, so perhaps you need to split else where? or perhaps you need to try all combinations of i,j for splitting.

Good luck.

Rusty Rob
  • 16,489
  • 8
  • 100
  • 116
0

Suppose we chose x as the maximal number in the reduced array we can take the array and decrease all its larger elements to x and calculate the cost of these operations and their number then we can try joining greedily the cheapest(in cost) subsets of these elements until the number of operations is smaller than N.If cost becomes larger than M then the maximal element must be larger in the reduced array.As for x we can easily binary search it.

GEP
  • 311
  • 3
  • 12
0

Honestly, I have a decent suspicion that finding the optimum solution to this problem is NP-hard. I admit this isn't particularly helpful, but it might put you in a different perspective on how to approach the larger issue that this is a part of.

eh9
  • 7,340
  • 20
  • 43