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

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

Source

题目链接:POJ 2976

题意:给你N个物品,每一个物品有它的属性ai与bi,求丢掉K个物品后使留下来的N-K个物品的${\Sigma a_i} \over {\Sigma b_i}$最大化。

如果没听过01分数规划可以先看这篇文章:传送门1,传送门2,然后我想说的是其中对$r={{\Sigma a_i*x_i} \over {\Sigma b_i*x_i}}$的移项变形可以得到:$0={\Sigma a_i*x_i}-r*{\Sigma b_i*x_i}$

然后就是这里搞了一会儿才弄明白(数学渣没办法),想一想是不是很想熟悉的二次函数$y=ax^2+bx+c$的形式,有时候我们也利用$0=ax^2+bx+c$来推导二次函数,可以发现后者是前者的特殊情况,当y=0时前者便成了后者,或者说后者求出来的解是在自变量轴上的交点。那么上面那个函数同理,若把0改成F(r),则可以发现这是一个图像,由于bixi大于等于0,因此至少是关于r递减,又由于这个变量又受x_i集合的取值影响,这个图像实际上就像我们要使得$0={\Sigma a_i*x_i}-r*{\Sigma b_i*x_i}$成立,显然需要当$r=r_i$的时候$F(r)=0$,但是这样的取值$r_i$可以有很多个,那么我们要找到最靠右的那个点作为答案即可。当然这题还用到了贪心的思想,因为取哪个是随意的,当然取每一次取最优的

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1010;
const double eps = 1e-5;
double a[N], b[N], d[N]; int main(void)
{
int n, k, i;
while (~scanf("%d%d", &n, &k) && (n | k))
{
for (i = 0; i < n; ++i)
scanf("%lf", a + i);
for (i = 0; i < n; ++i)
scanf("%lf", b + i);
double L = 0, R = 1e9 + 7, ans = 0;
int res = n - k;
while (fabs(R - L) >= eps)
{
double mid = (L + R) / 2.0;
for (i = 0; i < n; ++i)
d[i] = a[i] - mid * b[i];
sort(d, d + n, greater<double>());
double temp = 0;
for (i = 0; i < res; ++i)
temp += d[i];
if (temp > 0)
{
L = mid;
ans = mid;
}
else
R = mid;
}
printf("%.0f\n", 100.0 * ans);
}
return 0;
}

POJ 2976 Dropping tests(01分数规划入门)的更多相关文章

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

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

  2. POJ 2976 Dropping tests 01分数规划

    给出n(n<=1000)个考试的成绩ai和满分bi,要求去掉k个考试成绩,使得剩下的∑ai/∑bi*100最大并输出. 典型的01分数规划 要使∑ai/∑bi最大,不妨设ans=∑ai/∑bi, ...

  3. $POJ$2976 $Dropping\ tests$ 01分数规划+贪心

    正解:01分数规划 解题报告: 传送门! 板子题鸭,,, 显然考虑变成$a[i]-mid\cdot b[i]$,显然无脑贪心下得选出最大的$k$个然后判断是否大于0就好(,,,这么弱智真的算贪心嘛$T ...

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

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

  5. POJ 2976 Dropping tests(分数规划)

    http://poj.org/problem?id=2976 题意: 给出ai和bi,ai和bi是一一配对的,现在可以删除k对,使得的值最大. 思路: 分数规划题,可以参考<挑战程序竞赛> ...

  6. [poj 2976] Dropping tests (分数规划 二分)

    原题: 传送门 题意: 给出n个a和b,让选出n-k个使得(sigma a[i])/(sigma b[i])最大 直接用分数规划.. code: //By Menteur_Hxy #include & ...

  7. Dropping tests(01分数规划)

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8176   Accepted: 2862 De ...

  8. [poj2976]Dropping tests(01分数规划,转化为二分解决或Dinkelbach算法)

    题意:有n场考试,给出每场答对的题数a和这场一共有几道题b,求去掉k场考试后,公式.的最大值 解题关键:01分数规划,double类型二分的写法(poj崩溃,未提交) 或者r-l<=1e-3(右 ...

  9. POJ2976 Dropping tests —— 01分数规划 二分法

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

  10. poj Dropping tests 01分数规划---Dinkelbach算法

    果然比二分要快将近一倍.63MS.二分94MS. #include <iostream> #include <algorithm> #include <cstdio> ...

随机推荐

  1. form 表单 和 jQuery HTML / CSS 方法($().html 类似的样式)

    1 有关链接 :http://www.runoob.com/tags/tag-form.html https://www.cnblogs.com/Jxwz/p/4509618.html https:/ ...

  2. jrtplib库使用简解

    RTP有效载荷类型即时间截解释 =============================== https://www.cnblogs.com/wyqfighting/archive/2013/03/ ...

  3. STL之deque用法

    deque:双端队列 底层是一个双向链表. 常用的有队列的尾部入队.首部出队. 普通队列:queuequeue 模板类的定义在<queue>头文件中.与stack 模板类很相似,queue ...

  4. CSS+JS实现流星雨动画

    引言 平常会做一些有意思的小案例练手,通常都会发到codepen上,但是codepen不能写分析.        所以就在博客上开个案例分享系列,对demo做个剖析.目的以分享为主,然后也希望各路大神 ...

  5. 第二章JavaScript 函数和对象

    1 JavaScript 函数 1.1 声明函数的方式 function 关键字 匿名函数方式(表达式方式) Function 构造函数方式 1.2 参数问题 形参和实参数量问题 可选形参(参数默认值 ...

  6. 用express框架实现反向代理

    目前很多公司开发都是前后台分离开发,于是我用node起了一个服务,用node中的express框架实现了反向代理.(通俗易懂的讲就是我在我的电脑访问不到后台同事的电脑接口,这样做以后就可以在我本地访问 ...

  7. 在windows server 2008 64位服务器上配置php环境

    1.安装windows2008 R2 46位 安装2008 R2 关键步骤,网上有很多诸如此类的安装介绍.在些南昌网站建设公司百恒网络工程师就不作详细介绍.关键是要选择适合实际应用的部署.    2. ...

  8. PHP 根据IP获取地理位置

    /** * 根据用户IP获取用户地理位置 * $ip 用户ip */ function get_position($ip){ if(empty($ip)){ return '缺少用户ip'; } $u ...

  9. CodeForces:#448 div2 a Pizza Separation

    传送门:http://codeforces.com/contest/895/problem/A A. Pizza Separation time limit per test1 second memo ...

  10. 记忆化搜索:POJ1088-滑雪(经典的记忆化搜索)

    skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑 ...