Блог пользователя code_hard123

Автор code_hard123, история, 7 лет назад, По-английски

Given N points in two dimensional space and Q queries. In each query, given two non negative integers a , b. Find the point (x,y) such that F(x,y) is minimum, where F(x,y) = a * x + b * y

Constraints:
1 <= N <= 10^6
1 <= Q <= 10^5
0 <= a , b <= 10^9
0 <= |x| , |y| <= 10^9

Thank You!

  • Проголосовать: нравится
  • +23
  • Проголосовать: не нравится

»
7 лет назад, # |
Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

Choose for every given x minimal y among all points with this x. Since a > 0, b > 0 it`s reasonable to choose for bigger values of x smaller values of y. I mean the sequence of chosen y-values must be monotonically decreasing: . Consider two functions: f(x) = ax and g(x) = byx, where yx denotes the y-value corresponding to the given x. One monotonically increasing while another monotonically decreasing. Seems like minimum of their sum may be found through ternary search by x.

»
7 лет назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

Let v  <  u if v.x  <  u.x and v.y  <  u.y, consider the smallest points, red points in image, and apply a ternary search to minimize the function since necessarily with a, b  ≥  0 if u < v then A.u <= A.v (A = (a, b) and . is dot product), I hope you understand, I'm not good at explaining xD.

  • »
    »
    7 лет назад, # ^ |
    Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

    Consider any red point and any a and b  ≥  0, prove that the function is convex (respect to red points).

    spoiler