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

  1. 3 1
  2. 5 0 2
  3. 5 1 6
  4. 4 2
  5. 1 2 7 9
  6. 5 6 7 9
  7. 0 0

Sample Output

  1. 83
  2. 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$可以有很多个,那么我们要找到最靠右的那个点作为答案即可。当然这题还用到了贪心的思想,因为取哪个是随意的,当然取每一次取最优的

代码:

  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstdlib>
  5. #include <sstream>
  6. #include <numeric>
  7. #include <cstring>
  8. #include <bitset>
  9. #include <string>
  10. #include <deque>
  11. #include <stack>
  12. #include <cmath>
  13. #include <queue>
  14. #include <set>
  15. #include <map>
  16. using namespace std;
  17. #define INF 0x3f3f3f3f
  18. #define LC(x) (x<<1)
  19. #define RC(x) ((x<<1)+1)
  20. #define MID(x,y) ((x+y)>>1)
  21. #define CLR(arr,val) memset(arr,val,sizeof(arr))
  22. #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
  23. typedef pair<int, int> pii;
  24. typedef long long LL;
  25. const double PI = acos(-1.0);
  26. const int N = 1010;
  27. const double eps = 1e-5;
  28. double a[N], b[N], d[N];
  29.  
  30. int main(void)
  31. {
  32. int n, k, i;
  33. while (~scanf("%d%d", &n, &k) && (n | k))
  34. {
  35. for (i = 0; i < n; ++i)
  36. scanf("%lf", a + i);
  37. for (i = 0; i < n; ++i)
  38. scanf("%lf", b + i);
  39. double L = 0, R = 1e9 + 7, ans = 0;
  40. int res = n - k;
  41. while (fabs(R - L) >= eps)
  42. {
  43. double mid = (L + R) / 2.0;
  44. for (i = 0; i < n; ++i)
  45. d[i] = a[i] - mid * b[i];
  46. sort(d, d + n, greater<double>());
  47. double temp = 0;
  48. for (i = 0; i < res; ++i)
  49. temp += d[i];
  50. if (temp > 0)
  51. {
  52. L = mid;
  53. ans = mid;
  54. }
  55. else
  56. R = mid;
  57. }
  58. printf("%.0f\n", 100.0 * ans);
  59. }
  60. return 0;
  61. }

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. 在ListBox控件间交换数据

    实现效果: 知识运用: ListBox控件的SelectedItem属性 //获取或设置ListBox控件中当前选定的数据项 public Object SelectedItem{ get;set; ...

  2. python_30_购物车复习

    prodcut_list=[ ('Iphone', 5800), ('Mac Pro', 9800), ('Bike', 800), ('Watch', 10600), ('Coffee', 31), ...

  3. 小w的糖果

    题目连接 : https://ac.nowcoder.com/acm/contest/923/C 算是一道找规律的题了,因为后一个人会比前一个人多,可以理解成后一个人要继承前一个人,sum为当前糖果数 ...

  4. C#事件与接口编程实例

    很多初学c#的朋友对于事件与接口感到迷惑不解,不明白它们之间的关系,下面我就用实例来简单的分析讲解一下. 事件,用event修饰符来代表一个事件,我们要创建一个C#事件必须按以下顺序来扫行: 1,创建 ...

  5. DOTA自走棋卡牌及搭配阵容

    这个游戏其实就根炉石jjc和A牌轮抽一样,前期要找着质量牌抓,保证你至少不漏.根据你的需求补一些你不会上场的阵容组件,最后根据你的组件和核心紫卡来哪张来决定打什么.另外也要考虑场上另外几家,如果有一家 ...

  6. Symmetric Difference-freecodecamp算法题目

    Symmetric Difference 1.要求 创建一个函数,接受两个或多个数组,返回所给数组的对等差分(symmetric difference) 例子:给出两个集合 (如集合 A = {1, ...

  7. Qt之QThread随记

    这是一篇随记,排版什么的就没有那么好了:) 首先要知道,一个线程在资源分配完之后是以某段代码为起点开始执行的,例如STL内的std::thread,POSIX下的pthread等,都是以函数加其参数之 ...

  8. mysql 删除 一天前 创建 的数据,计算时间差

    DELETE from table_name WHERE TIMESTAMPDIFF(SECOND ,CREATE_TIME,now() ) > 24*60*60 https://www.cnb ...

  9. Python中列表的深浅拷贝

    copy_lst = [ ('py对象三要素',), ('== 比较运算符',), ('is 身份运算符',), ('小数据池',), ('列表的浅拷贝',), ('列表的深拷贝',), ] py对象 ...

  10. 【Umezawa's Jitte】真正用起来svn来管理版本

    之前用过一次 但是没有真正的用起来 只是知道了一些基本概念 好了 决定开始真正的用这个svn了 参考大神http://www.cnblogs.com/wrmfw/archive/2011/09/08/ ...