Description
In a certain course, you take ntests. If you get ai out of bi questionscorrect on test i, your cumulative average is defined to be
.
Given your test scores and a positiveinteger k, determine how high you can make your cumulative average ifyou are allowed to drop any k of your test scores.
Suppose you take 3 tests with scores of5/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 .
Input
The input test file will contain multipletest cases, each containing exactly three lines. The first line contains twointegers, 1 ≤ n ≤ 1000 and 0 ≤ k < n. The second linecontains n integers indicating ai for all i.The third line contains n positive integers indicating bifor 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.
Output
For each test case, write a single linewith the highest cumulative average possible after dropping k of thegiven test scores. The average should be rounded to the nearest integer.
Sample Input
3 1
5 0 2
5 1 6
4 2
1 2 7 9
5 6 7 9
0 0
Sample Output
83
100
Hint
To avoid ambiguities due to roundingerrors, the judge tests have been constructed so that all answers are at least0.001 away from a decision boundary (i.e., you can assume that the average isnever 83.4997).
思路:
给定ai和bi可以去掉k个(ai,bi)对。
y=100*sigma(ai)/sigma(bi)
t(y)=100*sigma(ai)-y*sigma(bi)
求t(y0)=0;
当t(y)<0时,y太大,y0<y;
当t(y)>0时,y太小,y<y0;
所以可以二分y的值。
去哪k个更优了?要使y更大就应该是t(y)>0这种情况更可能的出现,所以对100*ai-bi排序,去掉小的k个。使t(y)>0城里更有可能依然需要确定一个贪心策略,每次贪心地去掉那些对正确率贡献小的考试。如何确定某个考试[a_i, b_i]对总体准确率x的贡献呢?a_i / b_i肯定是不行的,不然例子里的[0,1]会首当其冲被刷掉。在当前准确率为x的情况下,这场考试“额外”对的题目数量是a_i – x * b_i,当然这个值有正有负,恰好可以作为“贡献度”的测量。于是利用这个给考试排个降序,后k个刷掉就行了。
代码:
#include<iostream>
#include<algorithm>
using namespace std;
int n,k;
double a[1010],b[1010],score[1010];
const double eps=1e-4;//题目有精度提示
int main()
{
while(cin>>n>>k&&n!=0)//不能加:&&K!=0
{
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
cin>>b[i];
}
double right=100,left=0;
while(right-left>eps)//不能写成>0,否则会死循环
{
double mid=(left+right)/2;//二分搜索
for(int i=0;i<n;i++)
{
score[i]=100*a[i]-mid*b[i];
}
sort(score,score+n);//先排序
double sum=0;
for(int i=k;i<n;i++)
{
sum+=score[i];
}
if(sum>0)
{
left=mid;
}
else
{
right=mid;
}
}
cout<<(long long)(left+0.5)<<endl;//输出小数最接近的整数的方法
}
}