题目:
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
.
Input
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 npositive 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.
Output
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.
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 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和K,代表有N个成绩,让你去掉的成绩个数;
下面的两行代表每一个考试的成绩,让你求出来去除掉K个成绩后 的最大值;
思路:
假设 ai / bi >=x,那么ai - x*bi >= 0;可以直接对于x 进行二分搜索,计算出每一个成绩对应的ai - x * bi 的值存成 s[ i ] ;
那么只需要把计算出来的s数组进行排序,选择出来最大的前K个数值即可,那么这K个数值的和就是最大的结果;
注意输出结果即可;
扩展:
关于01分数规划,查看了几篇博客才有了一点头绪,那么就用来理解这一道题的代码吧;
令F(x)=ai - x*bi,那么这就可以看成一个一元一次的方程式:F(x)=A - x*B,即F(x)= - B *x + A;
那么我们可以知道这条直线和Y轴的交点是 A(A >=0,因为 A = ai ),和X轴的交点是A / B ,斜率是 - B(小于0);
要求出来 x 的最大值,那么这条直线和X轴的交点就是最大值;
那么我们就可以来枚举 x 的值,找出来一条垂直于X轴的直线L,看直线 L 和直线F(x)的交点;
如果交点的值在X轴上边,那么这个 x 的值就是小于最大解;
如果交点的值在X轴下边,那么这个 x 的值就是大于最大解;
这个就是二分的依据了,成功的解决了代码中二分依据的问题;
代码如下:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=1010;
int n,k;
int a[N],b[N];
double s[N];
double low,high,mid;
int judge(double x)
{
for(int i=1;i<=n;i++)
{
s[i]=a[i]-x*b[i];
}
sort(s+1,s+n+1);
double ans=0;
for(int i=n;i>k;i--)///选择最大的K个值;
{
ans+=s[i];
}
return ans>=0;
}
void solve()
{///01分数规划;
low=0;
high=1;
while(high-low>1e-6)///注意判断条件;
{
mid=(low+high)/2.0;
if(judge(mid))
low=mid;
else
high=mid;
}
printf("%.0lf\n",low*100);///注意输出;
return ;
}
int main()
{
while(~scanf("%d%d",&n,&k))
{
if(n==0&&k==0)///输入结束;
break;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}
solve();
}
return 0;
}