题目链接:http://poj.org/problem?id=2976

Dropping tests
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20830   Accepted: 7052

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 npositive 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 K  下面有两行 每行有N个数  第一行代表N个a[i]   第二行代表N个b[i]   问你按照上面的公式算  去掉K个  得到的最大结果是多少

思路:自己并没有想到怎么二分 ,自己想到的是怎么贪心,但是感觉不行,就没去尝试了,其实想的到二分答案,但是怎么判断答案是否成立就没有想到了,这里用到的一种思想,分析表达式

以前我都是看到表达式就看一下的,从来没有给它仔细化简分析啥的  这一题算是一个教训吧!  那么下面看一下这个表达式到底怎么回事

令r = ∑a[i] * x[i] / (b[i] * x[i])  则必然∑a[i] * x[i] - ∑b[i] * x[i] * r= 0;(条件1)并且任意的 ∑a[i] * x[i] - ∑b[i] * x[i] * max(r) <= 0  (条件2,只有当∑a[i] * x[i] / (b[i] * x[i]) = max(r) 条件2中等号才成立)然后就可以枚举r , 对枚举的r, 求Q(r) = ∑a[i] * x[i] - ∑b[i] * x[i] * r  的最大值,  为什么要求最大值呢?  因为我们之前知道了条件2,所以当我们枚举到r为max(r)的值时,显然对于所有的情况Q(r)都会小于等于0,并且Q(r)的最大值一定是0.而我们求最大值的目的就是寻找Q(r)=0的可能性,这样就满足了条件1,最后就是枚举使得Q(r)恰好等于0时就找到了max(r)。而如果能Q(r)>0 说明该r值是偏小的,并且可能存在Q(r)=0,而Q(r)<0的话,很明显是r值偏大的,因为max(r)都是使Q(r)最大值为0,说明不可能存在Q(r)=0了。
注意:二分真的很气人啊,这题用了以前经常用的二分板子,竟然错了。。。  真的不明白为啥,就是二分最后的结果是l  还是r   应该是这题有点问题,下面wa的代码也给出来吧

wa的:

#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
typedef long long ll;
const int maxn=1000+5;
const double inf=1e15;
const double eps=1e-7;
int N,K;
ll a[maxn],b[maxn];
double c[maxn];
bool cmp(const double x,const double y)
{return x>y;
}
bool judge(double mid)
{for(int i=0;i<N;i++)//找到最优的解
    {c[i]=a[i]-mid*b[i];}sort(c,c+N,cmp);double sum=0;for(int i=0;i<N-K;i++) sum+=c[i];return sum>=0;
}
int main()
{while(cin>>N>>K){if(N==0&&K==0) break;for(int i=0;i<N;i++) cin>>a[i];for(int i=0;i<N;i++) cin>>b[i];double l=0,r=inf;double ans;while(r-l>=eps){double mid=(r+l)/2;//cout<<mid<<endl;if(judge(mid)){ans=mid;l=mid+eps;}else r=mid;//cout<<"ans: "<<ans<<endl;//cout<<"l: "<<l<<endl;//cout<<"r: "<<r<<endl;
       }printf("%.0lf\n", ans * 100);/*double mid;while(r-l>=eps){mid=(l+r)/2;if(judge(mid))l=mid;else r=mid;cout<<"l: "<<l<<endl;cout<<"r: "<<r<<endl;}printf("%.0lf\n",r*100);//printf("%.0lf\n", ans * 100);//cout<<(int)(ans*100+0.5)<<endl;
*/}return 0;
}/*
*/

ac的:

#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
typedef long long ll;
const int maxn=1000+5;
const double inf=1e15;
const double eps=1e-7;
int N,K;
ll a[maxn],b[maxn];
double c[maxn];
bool cmp(const double x,const double y)
{return x>y;
}
bool judge(double mid)
{for(int i=0;i<N;i++)//找到最优的解
    {c[i]=a[i]-mid*b[i];}sort(c,c+N,cmp);double sum=0;for(int i=0;i<N-K;i++) sum+=c[i];return sum>=0;
}
int main()
{while(cin>>N>>K){if(N==0&&K==0) break;for(int i=0;i<N;i++) cin>>a[i];for(int i=0;i<N;i++) cin>>b[i];double l=0,r=inf;double ans;while(r-l>=eps){double mid=(r+l)/2;//cout<<mid<<endl;if(judge(mid)){ans=mid;l=mid+eps;}else r=mid;//cout<<"ans: "<<ans<<endl;//cout<<"l: "<<l<<endl;//cout<<"r: "<<r<<endl;
       }printf("%.0lf\n", r * 100);/*double mid;while(r-l>=eps){mid=(l+r)/2;if(judge(mid))l=mid;else r=mid;cout<<"l: "<<l<<endl;cout<<"r: "<<r<<endl;}printf("%.0lf\n",r*100);//printf("%.0lf\n", ans * 100);//cout<<(int)(ans*100+0.5)<<endl;
*/}return 0;
}/*
*/

转载于:https://www.cnblogs.com/caijiaming/p/10387166.html

Dropping tests相关推荐

  1. Bailian4145 放弃考试 POJ2976 ZOJ3068 Dropping tests【二分法+01分数规划】

    4145:放弃考试 总时间限制: 1000ms 内存限制: 65536kB 描述 在一门课程中,一共有n场考试.假如你在i场考试中可以答对bi道题中的ai道,那么你的累计平均分定义为:100·Σai/ ...

  2. 二分+01分数规划+最大化平均值 Dropping tests POJ - 2976

    题意: 给你若n个分数,分子a[i]a[i]a[i],分母b[i]b[i]b[i],使满足公式100⋅∑i=1nai∑i=1nbi100\cdot\tfrac{\sum_{i=1}^{n} a_{i} ...

  3. 【POJ - 2976】【ZOJ - 3068】【SCU - 2992】Dropping tests (01分数规划)

    题干: In a certain course, you take n tests. If you get ai out of bi questions correct on test i, your ...

  4. POJ2976——Dropping tests(0/1分数规划)

    传送门 最简单的分数规划 对于最终答案ans,有 ans=Σai∗100Σbians=\frac{Σa_i*100}{Σb_i}ans=Σbi​Σai​∗100​ 则 Σbi∗ans=Σai∗100Σ ...

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

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

  6. [POJ2976] Dropping tests

    传送门:>Here< 题意:给出长度相等的数组a和b,定义他们的和为$\dfrac{a_1+a_2+...+a_n}{b_1+b_2+...+b_n}$.现在可以舍弃k对元素(一对即$a[ ...

  7. poj2976 Dropping tests(01分数规划 好题)

    https://vjudge.net/problem/POJ-2976 又是一波c++AC,g++WA的题.. 先推导公式:由题意得 Σa[i]/Σb[i]<=x,二分求最大x.化简为Σ(a[i ...

  8. POJ-2976 Dropping tests 01分数规划

    题目链接:http://poj.org/problem?id=2976 很典型的01分数规划,sort+二分即可.注意精度问题,这种四舍五入的问题一般都是两种处理方法:1.printf("% ...

  9. poj2976 Dropping tests

    01分数规划裸题 为毛二分一定要打成r=mid这么恶心啊 #include<cstdio> #include<iostream> #include<cstring> ...

最新文章

  1. 日志——Vue.js开发在线简历生成器
  2. 使用苹果版博客编辑器发布的文章
  3. Spring Security 实战干货:客户端OAuth2授权请求的入口在哪里
  4. 易创课堂武汉站-NTES@百位创业者智慧众筹
  5. 关于Java空指针的控制(转)
  6. JAVA Collection笔记(2012/9/19)
  7. zbox mysql_20190213云服务器部署禅道
  8. 嵌入式linux文件系统类型,嵌入式Linux 的Cramfs 根文件系统配置的解决方案
  9. PPP Over Frame Relay配置
  10. 三只松鼠7月12日登陆创业板 募资60亿元
  11. 世界知名网站的技术实现(转)
  12. oracle没有网卡驱动,联想台式机网卡驱动,手把手教你联想台式机网卡驱动
  13. 计算机系统管理思维导图,个人管理系统思维导图.pdf
  14. 移动联通基站定位API以及电信基站定位API
  15. ue4之将Sequence嵌入蓝图
  16. 企业微信有权限查看通讯录吗?
  17. 我和王争学设计模式|建造者模式
  18. 【IM集成攻略】手把手教你环信对接离线推送,再搞不定把你头打掉
  19. O2O优惠券核销-数据分析
  20. 第六次作业·团队作业

热门文章

  1. 云计算-My Future, The IT's Future
  2. 用队列和栈的知识点解决迷宫问题
  3. jquery remove()不兼容问题解决方案
  4. 推荐系统中粗排扮演的角色和算法发展历程
  5. 测试开发之编写测试用例
  6. python 中的__future__模块
  7. c 语言 宏 可变 参数,利用C可变参数和宏定义来实现自己的日志系统
  8. chartxy 柱状图_关于Chart柱状图的使用,有问题
  9. ios项目中使用gcd的场景_iOS中超级超级详细介绍GCD
  10. cocos creator 多张图片 椭圆运动_信阳液位测量图片