Algorithm to maximize the sum of product of elements of two arrays

前端 未结 1 1273
粉色の甜心
粉色の甜心 2021-01-25 07:16

There was a question in a contest which requires calculating the performance of the class which contains only Maths and Bio subject. So, there are \'n\' no. of maths student &am

相关标签:
1条回答
  • 2021-01-25 07:53

    We have Sum(Abs(o[i])) == m m > 0, and a[i], b[i], m fixed.

    Sum( (a[i] + o[i]) * b[i]) == Sum(a[i] * b[i]) + Sum(o[i] * b[i])
    

    So to maximize it, we just have to maximize

    Sum(o[i] * b[i])
    

    And we have

    o[i] * b[i] <= Abs(o[i] * b[i])
    

    And as we can choose sign of o[i], we can maximize instead

    Sum(Abs(o[i] * b[i]))
    

    which is

    Sum(Abs(o[i]) * Abs(b[i]))
    

    and

    Max(Sum(Abs(o[i]) * Abs(b[i]))) <= m * Max(Abs(b[i]))
    

    and with j so that b[j] == Max(Abs(b[i])), o[j] == sign(b[j]) * m, o[i] == 0, we have

    Sum(o[i] * b[i]) == m * Max(Abs(b[i]))
    

    So you have to find maximum of the both array (of absolute value).

    So in your example

    Maths Score :  5  7  (4+m) -3
    Bio Score   : -2  3  9      2
    

    And for other example:

    Maths Score :  5  7  (4-m) -3
    Bio Score   : -2  3  -9     2
    
    0 讨论(0)
提交回复
热议问题