Dropping tests(二分查找平均值)
来源:互联网 发布:java script else 编辑:程序博客网 时间:2024/06/14 09:03
原题链接
Dropping tests
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9915 Accepted: 3459
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
这个问题在《挑战程序设计竞赛》上有完全类似的问题就是求最大化平均值,这类问题可以归结为一类问题那就是有n个物品的重量和价值分别是wi和vi,从中选出k个物品使得单位重量的价值最大。
那么首先我们可以规定C(x)的意思为可以选择使得单位重量的价值不小于x。假设选了k个物品。那么必有这k个物品有如下图的关系
要得到这个k个值所以这个时候只需要把进行了v-x*w运算后的值进行从大到小的排序后选择前k个即可
//http://poj.org/problem?id=2976#include <algorithm>#include <iostream>#include <utility>#include <sstream>#include <cstring>#include <cstdio>#include <vector>#include <queue>#include <stack>#include <cmath>#include <map>#include <set>using namespace std;typedef long long ll;const int MOD = int(1e9) + 7;//int MOD = 99990001;const int INF = 0x3f3f3f3f;const ll INFF = (~(0ULL)>>1);const double EPS = 1e-9;const double OO = 1e20;const double PI = acos(-1.0); //M_PI;const int fx[] = {-1, 1, 0, 0};const int fy[] = {0, 0, -1, 1};const int maxn=1000 + 5;struct node{int u,d;}a[maxn];int n,k,MAX;double shengyu[maxn];//判断下当前的这个平均数是否可以bool cmp(double x,double y){ return x>y;}bool can(double mid){ for(int i=0;i<n;i++) shengyu[i]=a[i].u - mid*a[i].d; sort(shengyu,shengyu+n,cmp); //计算从大到小前k项的和 double sum=0; for(int i=0;i<(n-k);i++) sum+=shengyu[i]; return sum>=0;}void solve(){ double l=0,r=1; for(int i=0;i<100;i++){ double mid=(l+r)/2; //cout << "mid:" << mid << endl; if(can(mid)) l=mid; else r=mid; } printf("%d\n",(int)(r*100 + 0.5));}int main(){ while(scanf("%d%d",&n,&k)==2 && (n+k)){ //k=n-k;//把题目修改成选择k个科目使得平均分最大,并且这个时候最少还会选一门 for(int i=0;i<n;i++) scanf("%d",&a[i].u); for(int i=0;i<n;i++) scanf("%d",&a[i].d); solve(); } return 0;}
- Dropping tests(二分查找平均值)
- POJ - 2976 Dropping tests(二分查找,最大化平均值)
- POJ 2976 Dropping tests(二分查找 最大化平均值)
- POJ2976 Dropping tests 最大化平均值(二分)
- POJ - 2976 Dropping tests(二分搜索:最大化平均值)
- POJ2976 Dropping tests (最大化平均值/二分)
- POJ 2976 Dropping tests 【二分:最大化平均值】
- (最大化平均值)poj2976,Dropping tests
- POJ 2976 Dropping tests (最大化平均值)
- poj2976 Dropping tests(二分法:最大化平均值)
- poj 2976 Dropping tests (二分查找)
- POJ 2976 Dropping tests(二分搜索,最大化平均值)
- POJ2976-Dropping tests-最大化平均值
- POJ 2976 Dropping tests(贪心+二分)
- [POJ 2976] Dropping tests (二分答案)
- Poj 2976 Dropping tests(二分)
- 【POJ】2976 Dropping tests(二分)
- poj2976 Dropping tests(二分答案)
- 详解内部类
- 【Android】Android Studio 2上利用NDK进行OpenCV 3.1开发(注意点)
- MTK平台camera菜单项,相互限制修改
- tableview,collectionview上边有白边——的解决办法(iOS调试)
- C++拷贝构造函数 参数类型为什么是引用类型 ...
- Dropping tests(二分查找平均值)
- 状态 组合情况 太多的处理方式
- 新手指南:手把手教你如何搭建自己的渗透测试环境
- 微信会员卡对接CRM后台(java)
- 第2周 实践项目3--体验复杂度(1)
- SQL操作 - 插入时更新数据
- 图片语录
- 被忽视的 partition 算法
- maven工程打包遇到Unable to locate Spring NamespaceHandler for XML schema namespace问题