题目描述:
In a certain course, you take n tests. If you get ai out of bi questions correct on test i, your cumulative average is defined to be
Given your test scores and a positive integer k, determine how high you can make your cumulative average if you are allowed to drop any k of your test scores.
Suppose you take 3 tests with scores of 5/5, 0/1, and 2/6. Without dropping any tests, your cumulative average is
However, if you drop the third test, your cumulative average becomes
输入:
The input test file will contain multiple test cases, each containing exactly three lines. The first line contains two integers, 1 ≤ n ≤ 1000 and 0 ≤ k < n. The second line contains n integers indicating ai for all i. The third line contains n positive integers indicating bi for all i. It is guaranteed that 0 ≤ ai ≤ bi ≤ 1, 000, 000, 000. The end-of-file is marked by a test case with n = k = 0 and should not be processed.
输出:
For each test case, write a single line with the highest cumulative average possible after dropping k of the given test scores. The average should be rounded to the nearest integer.
输入示例:
1 | 3 1 |
输出示例:
1 | 83 |
提示:
To avoid ambiguities due to rounding errors, the judge tests have been constructed so that all answers are at least 0.001 away from a decision boundary (i.e., you can assume that the average is never 83.4997).
题目大意:
有N个考试,每个考试有ai和bi两个值,最后成绩由上面的公式求得。幸运的是,可以放弃K个科目,求最大化最后的成绩。(输出乘100后四舍五入的结果)
思路:
由题意可知当,当n-k个科目组成最优解时,再增加别的科目,解一定不如原来的解,放弃掉k个科目是最好的方案。
于是,题目就变成了最小化平均值的问题,另外在注意下取整时的四舍五入就好了。
最小化平均值:
有n个物品的重量和价值分别为wi和vi,从中选择k个物品使得单位重量的价值最大。
对于这个问题,我们可以用二分搜索解决,先来看看判断条件:
设最大值为x,则需要满足 ∑vi / ∑wi >=x,把不等式进行变形,就得到了 ∑ ( vi - x wi )>=0,于是判断就成了,对 vi-xwi 的值进行排序之后贪心进行选择,判断前n-k个的和是否不小于0。判断的复杂度是O(nlogn)。
1 | /* |