Dropping tests

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 15508 Accepted: 5418

Description

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 100*sum(ai)/sum(bi)

.

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


中文题意:

今年有 n 场 ACM-ICPC 竞赛,小明每场都有资格参加。第 i 场竞赛共有 b[i] 道题。小明预测第 i 场他能做出 a[i] 道题。为了让自己看着更“大佬”一些,小明想让自己平均做出的题数越大越好,也就是最大化大佬度,大佬度的定义如下:

为了达到这个目的,小明决定放弃 k 场比赛的参赛资格。请求出最大的大佬度。

例如有 3 场小型比赛,题数分别是 5 题、1 题、6 题,小明预测自己分别能做出 5 题、0题、2题。如果每场都参加,那么大佬度是 ,看着不怎么大佬。不过,如果放弃第 3 场比赛,那么大佬度就是 ,看着更加大佬了。

Input

输入测试文件含有多组测试,每组有 3 行。第一行有 2 个整数, 1 ≤ n ≤ 1000 和 0 ≤ k < n。第二行有 n 个整数,即每个 a[i]。第三行含有 n 个正整数 b[i]。保证 0 ≤ a[i] ≤ b[i] ≤ 1, 000, 000, 000。文件末尾由 n = k = 0 标识,并且不应该被处理。

Output

对于每组测试数据,输出一行整数,即放弃 k 场比赛后可能的最高大佬度。大佬度应该舍入到最近的整数。

解题心得:

  1. 其实就是一个最大化平均值的问题,最大化平均值二分的一种经典应用,先假设答案为ans,如果100 * sum(ai) / sum(bi) > ans,那么按照这种大于/小于的关系式来二分答案,但是在运算的时候需要转化一下关系式,可以转换成:100 * sum(ai) > sum(bi) * ans,按照乘法的分配律可以写成100 * sum(ai) > sum(bi * ans),然后移项得到关系式:100 * sum(ai) - sum(bi * ans) > 0,然后按照减法的分配律可以写成sum(100 * ai - bi * ans) > 0,这样可以先得出100 * ai-bi * ans,然后降序排序,算出前n - k个是否大于0,然后以此二分就行了。

#include <algorithm>
#include <cstring>
#include <stdio.h>
using namespace std;
typedef long long ll;
const int maxn = 1010;
ll n,k,a[maxn],b[maxn];
double c[maxn]; void init() {
for(int i=0;i<n;i++)
scanf("%lld",&a[i]);
for(int i=0;i<n;i++)
scanf("%lld",&b[i]);
} bool cmp(double a,double b) {
return a > b;
} bool checke(double va) {
for(ll i=0;i<n;i++)
c[i] = (double)a[i]*100.0 - va*b[i];
sort(c,c+n,cmp);
double sum = 0;
for(int i=0;i<n-k;i++)
sum += c[i];
return sum >= 0;
} double binary_search() {
double l,r;
l = 0, r = 1e18+100;
for(int i=0;i<100;i++) {//为了保证精度问题直接循环100次
double mid = (l + r) / 2.0;
if(checke(mid))
l = mid;
else
r = mid;
}
return l;
} int main() {
while(scanf("%lld%lld",&n,&k) && n|k) {
init();
double ans = binary_search();
ll int_ans = (ans + 0.5);
printf("%lld\n",int_ans);
}
return 0;
}

POJ:2976-Dropping tests(二分平均值)的更多相关文章

  1. POJ 2976 Dropping tests [二分]

    1.题意:同poj3111,给出一组N个有价值a,重量b的物品,问去除K个之后,剩下的物品的平均值最大能取到多少? 2.分析:二分平均值,注意是去除K个,也就是选取N-K个 3.代码: # inclu ...

  2. poj 2976 Dropping tests (最大化平均值:二分查找)

    #include<iostream> #include<algorithm> #include<stdio.h> #include<math.h> #d ...

  3. POJ 2976 Dropping tests (二分+贪心)

    题意:给定 n 个分数,然后让你去年 m 个分数,使得把剩下的所有的分子和分母都相加的分数最大. 析:这个题并不是分子越大最后结果就越大,也不是整个分数越大,最后结果就越大的,我们可以反过来理解,要去 ...

  4. 二分算法的应用——最大化平均值 POJ 2976 Dropping tests

    最大化平均值 有n个物品的重量和价值分别wi 和 vi.从中选出 k 个物品使得 单位重量 的价值最大. 限制条件: <= k <= n <= ^ <= w_i <= v ...

  5. POJ - 2976 Dropping tests && 0/1 分数规划

    POJ - 2976 Dropping tests 你有 \(n\) 次考试成绩, 定义考试平均成绩为 \[\frac{\sum_{i = 1}^{n} a_{i}}{\sum_{i = 1}^{n} ...

  6. POJ 2976 Dropping tests 【01分数规划+二分】

    题目链接:http://poj.org/problem?id=2976 Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  7. POJ 2976 Dropping tests 01分数规划 模板

    Dropping tests   Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6373   Accepted: 2198 ...

  8. POJ 2976 Dropping tests(01分数规划)

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:17069   Accepted: 5925 De ...

  9. POJ 2976 Dropping tests(01分数规划入门)

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11367   Accepted: 3962 D ...

  10. POJ 2976 Dropping tests (0/1分数规划)

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4654   Accepted: 1587 De ...

随机推荐

  1. python面试题——爬虫相关

    1.接触过几种爬虫模块 urllib.requests这两种爬虫模块. 2.robots协议是什么? 它就是一个防君子不防小人的协议,网站有一些数据不想被爬虫程序爬取,可以编写robots协议文件,明 ...

  2. css中的伪元素,我今天记住了!o~yeah

    对于伪类和伪元素,我如果要区别它们,一般是使用css中的手册来区分,平常时候也没特意去记,需要用到时打开手册“哦,这个是伪元素,这个是伪类”,我个人觉的某些东西你把它存在网上,不一定要存在头脑中.带着 ...

  3. CAD鼠标移动到对象时显示对象内容

    //定义事件         Editor ed = doc.Editor;         ed.PointMonitor += new PointMonitorEventHandler(ed_Po ...

  4. Java 空对象设计模式(Null Object Pattern) 讲解

    转自:http://www.cnblogs.com/haodawang/articles/5962531.html 有时候我们的代码中为避免 NullPointerException 会出现很多的对N ...

  5. bootstrap中文文档阅读记录

    2017年1月12日20:26:26http://v3.bootcss.com/components/#navbar

  6. 初识Python(三)

    一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在后续的代码中使用: 外层变量,可以被内层变量使用:内层变量,也可以被外层变量使用: 如下示例: #!/usr/bin/env pyt ...

  7. April 12 2017 Week 15 Wednesday

    Genius often betrays itself into great errors. 天才常被天才误. Genius can help us get greater achievements, ...

  8. Git-实验报告

    “Git 实战教程”实验报告 基本用法(下) 二.比较内容 1.比较提交 - Git Diff git diff命令的作用是比较修改的或提交的文件内容. 如何查看缓存区内与上次提交之间的差别呢?需要使 ...

  9. 动态数组第k小,Poj(1442)

    题目链接:http://poj.org/problem?id=1442 本来想复制一下,然后直接sort,结果T了. 在网上看了一下,有用两个队列做的,想了半天,没看懂什么意思.后来模拟一边,总算是懂 ...

  10. 从Java官网下载JDK1.6等低版本JDK

    今天在浏览Java官网的时候发现旧版本(1.8之前)的JDK安装包下载地址没有在下载页面明显的提供出来.个人通过在官网查看,发现oracle官方将旧版本的JDK全都放在Java Archive模块中了 ...