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   

.

.

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  tests with scores of /, /, and /. 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,  ≤ n ≤  and  ≤ k < n. The second line contains nintegers indicating ai for all i. The third line contains n positive integers indicating bi for all i. It is guaranteed that  ≤ ai ≤ bi ≤ , , , . The end-of-file is marked by a test case with n = k =  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


Sample Output


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

Source

 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
using namespace std;
#define N 1006
int n,k;
double ratio;
struct Node{
double a,b;
bool friend operator <(Node x,Node y){
return x.a-ratio*x.b>y.a-ratio*y.b;
}
}node[N];
bool solve(double mid){
ratio=mid;
sort(node,node+n);
double sum1=;
double sum2=;
for(int i=;i<n-k;i++){
sum1+=node[i].a;
sum2+=node[i].b;
}
return sum1/sum2>=mid;
} int main()
{
while(scanf("%d%d",&n,&k)== && n+k!=){
for(int i=;i<n;i++){
scanf("%lf",&node[i].a);
}
for(int i=;i<n;i++){
scanf("%lf",&node[i].b);
}
double low=;
double high=;
for(int i=;i<;i++){
double mid=(low+high)/;
if(solve(mid)){
low=mid;
}
else{
high=mid;
}
}
printf("%.0lf\n",high*);
}
return ;
}

乍看以为贪心或dp能解决,后来发现贪心策略与当前的总体准确率有关,行不通,于是二分解决。

依然需要确定一个贪心策略,每次贪心地去掉那些对正确率贡献小的考试。如何确定某个考试[a_i, b_i]对总体准确率x的贡献呢?a_i / b_i肯定是不行的,不然例子里的[0,1]会首当其冲被刷掉。在当前准确率为x的情况下,这场考试“额外”对的题目数量是a_i – x * b_i,当然这个值有正有负,恰好可以作为“贡献度”的测量。于是利用这个给考试排个降序,后k个刷掉就行了。

之后就是二分搜索了,从0到1之间搜一遍,我下面的注释应该很详细,不啰嗦了。

 #ifndef ONLINE_JUDGE
#pragma warning(disable : 4996)
#endif
#include <iostream>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std; #define MAX_N 1000
int n, k;
double x; // 搜索过程中的正确率
struct Test
{
int a, b;
bool operator < (const Test& other) const
{
return a - x * b > other.a - x * other.b; // 按照对准确率的贡献从大到小排序
}
};
Test test[MAX_N]; // 判断是否能够获得大于mid的准确率
bool C(double mid)
{
x = mid;
sort(test, test + n);
double total_a = , total_b = ;
for (int i = ; i < n - k; ++i) // 去掉后k个数计算准确率
{
total_a += test[i].a;
total_b += test[i].b;
} return total_a / total_b > mid;
} ///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
while (cin >> n >> k && (n || k))
{
for (int i = ; i < n; ++i)
{
cin >> test[i].a;
}
for (int i = ; i < n; ++i)
{
cin >> test[i].b;
} double lb = ; double ub = ;
while (abs(ub - lb) > 1e-)
{
double mid = (lb + ub) / ;
if (C(mid))
{
lb = mid; // 行,说明mid太小
}
else
{
ub = mid; // 不行,说明mid太大
}
} cout << fixed << setprecision() << lb * << endl;
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("out.txt");
#endif
return ;
}
///////////////////////////End Sub//////////////////////////////////

poj 2976 Dropping tests (二分搜索之最大化平均值之01分数规划)的更多相关文章

  1. poj 3111 K Best (二分搜索之最大化平均值之01分数规划)

    Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...

  2. POJ 2976 Dropping tests【二分 最大化平均值】

    题意:定义最大平均分为 (a1+a2+a3+---+an)/(b1+b2+---+bn),求任意去除k场考试的最大平均成绩 和挑战程序设计上面的最大化平均值的例子一样 判断是否存在x满足条件 (a1+ ...

  3. poj 2976 Dropping tests 二分搜索+精度处理

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8349   Accepted: 2919 De ...

  4. NYOJ 914 Yougth的最大化【二分/最大化平均值模板/01分数规划】

    914-Yougth的最大化 内存限制:64MB 时间限制:1000ms 特判: No 通过数:3 提交数:4 难度:4 题目描述: Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从 ...

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

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

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

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

  7. POJ - 2976 Dropping tests(01分数规划---二分(最大化平均值))

    题意:有n组ai和bi,要求去掉k组,使下式值最大. 分析: 1.此题是典型的01分数规划. 01分数规划:给定两个数组,a[i]表示选取i的可以得到的价值,b[i]表示选取i的代价.x[i]=1代表 ...

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

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

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

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

随机推荐

  1. 解决html5新标签【placeholder】低版本浏览器下不兼容问题

    placeholder属性是HTML5 中为input添加的.在input上提供一个占位符,文字形式展示输入字段预期值的提示信息(hint),该字段会在输入为空时显示. 实例:1 <input ...

  2. TypedArray和obtainStyledAttributes使用

    在编写Android自定义按钮示例基础上,如果要指定字体大小产生这样的效果: 其实是不需要自定义变量的,可以直接使用TextView的配置属性: <com.easymorse.textbutto ...

  3. POJ 2455Secret Milking Machine(二分+网络流之最大流)

    题目地址:POJ2455 手残真浪费时间啊..又拖到了今天才找出了错误..每晚两道题不知不觉又变回了每晚一道题...sad.. 第一次在isap中忘记调用bfs,第二次则是遍历的时候竟然是从1開始遍历 ...

  4. C语音指针Introduction.

    指针是C语言中广泛使用的一种数据类型. 运用指针编程是C语言最主要的风格之一.利用指针变量可以表示各种数据结构: 能很方便地使用数组和字符串: 并能象汇编语言一样处理内存地址,从而编出精练而高效的程序 ...

  5. URL与URI的区别

    URI—Universal Resource Identifier通用资源标志符Web上可用的每种资源如HTML文档.图像.视频片段.程序等都是一个来URI来定位的URI一般由三部组成①访问资源的命名 ...

  6. 关于ajax网络请求的封装

    // 封装的ajax网络请求函数// obj 是一个对象function AJAX(obj){ //跨域请求        if (obj.dataType == "jsonp") ...

  7. iOS ui界面vtf 开发

    addConstraints 添加约束的步奏 添加控件到view中 设置translateResizeLayoutintoautolayout = false 添加约束 注意 约束 : 出现 有父子关 ...

  8. myFocus焦点图插件

    注意 1.焦点图初始化ID和图片最外层ID保持一致 2.图片列表外面必须包裹一个div,且id必须为pic http://demo.jb51.net/js/myfocus/tutorials.html ...

  9. MVC上传相关

    1.上传大小设置 system.web节点,httpruntime节点加入maxRequestLength="4096",以K为单位,例子中大小限制为4M. 2.form提交htm ...

  10. CSS实现三角形效果

    类似三角形的形状的元素在网页中可能会用到,我们可以用图片或者CSS元素达到我们想要的效果.这里讲一下是讲自己使用HTML+CSS实现三角形的方式. 为了能够熟悉的使用HTML+CSS构建三角形,我们首 ...