I am trying to solve this problem: http://acm.tju.edu.cn/toj/showp2886.html
I have tried a few solutions, I will explain 2 of them. Note that both assume that cost(position) is a convex function, which means that it decreases towards the middle (in fact its somewhere towards median(supply points)), and the graph looks more or less like an U shape.
Finding the leftmost and rightmost supply point position that has cost(position) <= M. After I find the minimum and maximum, I try to see if I can make the interval a little larger (since the restaurant doesn't have to be places exactly on a supply point). This is a good solution but takes very much to compute the last bit. It fails on the tests where M is very large and there exists few supply points with minimum cost. Complexity: O(NlogN) + O(M). This solution below as it is gets Time Limit Exceeded, but I have tried another one where I compute in O(1) how much can I go in both directions and I get Wrong Answer.
Since the function is convex I can use a ternary search to find the minimum value. If the minimum is less than M, I then binary search the lower and the upper bound (meaning M) of the function. Complexity: O(NlogM) since for every O(logM) I compute the cost in O(N).
This is what I've tried and I still get "Wrong Answer", so I need some help since this is driving me nuts.
The problem is not really clear in specifications (or at least I think so) because when I first read it I didn't think the cost was computed from all supply points, but just from the nearest. The example cleared that out. Also, I don't know if my assumption of cost(position) function being convex is really true. But I've tried many examples and it seems so.
Thanks. Any help is appreciated. :D