寒假私训 ——二分 C - Dropping tests

Dropping tests

题目描述

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

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

解题思路

此题要查找sigma{ a[i ] /b[i ]} 的最大值。在进行二分的过程中,总会要先找到目标数据的范围,此题中很明显,位于0和1之间,因为a[i]<=b[i]。找到范围后,还要考虑chack()函数怎么写:

设最大值 为 x;
由sum{ a[i ]} /sum{b[i ]} = x,可得:
sum{ a[i] } = sum{ b[i] } *x;
sum{ a[i]} -sum{ b[i] * x } =0;
设 c [i] = a[i] - b[i] *x;
ans是c[i]的和,刨掉前K个小的,如果大于0,说明目标值在右部。
主函数部分参照二分模板。
反思:刚开始写的时候,迟迟想不到chack函数怎么写,后来想到了高中数学中的知识,发现这个题一点也不难,只要细心地寻找题目中函数最大值和mid的处理方法,就能很简单的想到解题思路。

正确代码

#include<stdio.h>
#include<iostream>
#include<algorithm>
#define maxn 1005
#define M 1e9+5
#define minn 1e-9
using namespace std;
typedef long long ll;
using namespace std;
double a[maxn],b[maxn],c[maxn];
int n,k;

double chack(double x){
     double ans=0;
     for(int i=0;i<n;i++){
               c[i]=a[i]-x*b[i];//将每一对的a[i]与x*b[i]的差值放入c[i]中,为了判断当前的mid(x)是否大于或小于目标值。
               ans+=c[i];  //求c[i]的和
     }
     sort(c,c+n);
     for(int i=0;i<k;i++){
               ans-=c[i];  //抛去差值过大的前k位,因为差值越大,说明越能影响目标值,会让目标值变小。
     }
     return ans;
}

int main()
{
    while(~scanf("%d%d",&n,&k)){
               if(n==0&&k==0)  break;
               for(int i=0;i<n;i++){
                              scanf("%lf",&a[i]);
               }
               for(int j=0;j<n;j++){
                              scanf("%lf",&b[j]);
               }
               double l=0,r=1.00,mid;
               while(r-l>minn){
                              mid=(l+r)/2.0;
                              if(chack(mid)>0)l=mid;  //sum>0,说明乘以mid以后,b[i]小于a[i],mid比预期的小。所以选择右部
                              else r=mid;
               }int num=l*100+0.5;
               printf("%d\n",num);
    }
    return 0;
}
发布了36 篇原创文章 · 获赞 1 · 访问量 1394

猜你喜欢

转载自blog.csdn.net/atnanajiang/article/details/104060717
今日推荐