本文介绍了最大化两个数组元素乘积之和的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

竞赛中有一个问题,要求计算仅包含数学和生物科目的班级的表现.因此,没有.数学系学生不生物学生.每个学生都有自己的分数.数学学生和生物学生的分数分别存储在数组mathScore []和bioScore中.整个班级的成绩计算如下:(mathScore [0] * bioScore [0] + mathScore [1] * bioScore [1] + ....... + mathScore [n-1] * bioScore [n-1])

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 & 'n' no. of bio student. Each student has an individual score. Scores of Maths students and Bio Students are stored in arrays mathScore[] and bioScore respectively. The performance of the whole class is calculated as follows:( mathScore[0]*bioScore[0] + mathScore[1]*bioScore[1] + ....... +mathScore[n-1]*bioScore[n-1] )

mathScore [0]代表第一位数学系学生的分数. bioScore [0]也是如此.

With mathScore[0] representing the score of first Maths student. And same is true for bioScore[0].

现在给定值'm',我们必须使课程的总分最大化.我们可以通过将任何候选人的分数增加或减少1(最多"m"次)来做到这一点.现在的问题是,您只能增加一组的分数.要么是数学的,要么是生物的.

Now given a value 'm', we have to maximize the overall score of the class. And we can do so by increasing or decreasing the score of any candidate by 1, a maximum of 'm' times. Now the catch is, you can only increase the score of only one group. Either Maths' or Bio's.

根据我的观点,现在解决这个问题需要两个步骤.首先是确定要选择哪个组,即Math或Bio.第二步是从所选的组中选择学生,以便增加或减少这些特定(所选)学生的分数将最大限度地提高表现.

Now coming to the solution, according to me, this question requires two step. First is to decide which group to chose, i.e. either Maths or Bio. And the second step is to select the students from that chosen group, so that increasing or decreasing those particular(selected) students' score will maximize the performance.

我已经尝试过通过以下方式考虑第一步:认为这是全班分数

I've tried thinking about the first step, this way:Consider this is the score of the class

Maths Score :  5  7  4 -3
Bio Score   : -2  3  9  2

其中m = 1;因此,我们将迭代数学分数数组.并同时比较了生物类学生的相应成绩.因此,我们选择了Maths数组.因为如果我们只需要增加一次.然后在Maths数组中增加4将是有益的. 因为它将使整体性能提高9.这是最大的优势.

With m=1;So, we'll iterate over the Maths score array. And meanwhile comparing the corresponding scores of Bio students. So, we chose the Maths array. Because if we need to increment only one time. Then increasing the 4 in the Maths array would be beneficial. 'Cause it will increase the overall performance by 9. Which is biggest.

第二步的方法相同.查找另一组中其对应分数最高的分数.增加该特定元素.

And same will be the approach for the second step. Find the score whose corresponding score in the other group is highest. Increase that particular element.

现在,这有点粗糙.但是它并不能解决所有的可能性.因此,我们将不胜感激.

Now, this is a bit rough idea. But its not working for all the possibilities. So, any help would be appreciated.

P.S.我不是大学生.这不是功课.

P.S. I'm not a college student. And this is no homework.

推荐答案

我们已修复Sum(Abs(o[i])) == m m > 0a[i]b[i]m.

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])

我们有

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

由于我们可以选择o[i]的符号,因此我们可以最大化

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

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

这是

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

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

以及j,这样我们就有b[j] == Max(Abs(b[i]))o[j] == sign(b[j]) * mo[i] == 0

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).

所以在您的示例中

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

还有另一个例子:

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

这篇关于最大化两个数组元素乘积之和的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 16:18