POJ 2976 Dropping tests 【01分数规划+二分】
题目链接:http://poj.org/problem?id=2976
Dropping tests
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 17838 | Accepted: 6186 |
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个物品,每个物品 i 有 a[ i ] 和 b[ i ] 两种属性,去掉K个物品,是的 Σai / Σbi 最大。
解题思路:
我们令 Σai / Σbi >= x, 然后二分x,而判断条件则移一下项得 Σai >= Σbi*x → Σai - Σbi*x >= 0,因为我们要去掉 K 个小的, 取N-K个使结果最大化的,所以用 令 p[ i ] = Σai - Σbi*x,对 p[ i ] 默认升序排序之后,第 K 到 N 个。判断这些数和是否满足不等式条件(大于等于0)
注意事项:
因为计算机的精度问题,要注意细节的处理,对浮点数的计算。(in代码)
AC code:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#define INF 0x3f3f3f3f
#define ll long long int
using namespace std; const int MAXN = ;
int N, K;
int a[MAXN], b[MAXN];
double p[MAXN]; ///这里的p一定要是浮点型,因为本来就是计算分式 bool ok(double x)
{
for(int i = ; i <= N; i++)
{
p[i] = a[i]-b[i]*x;
}
sort(p+, p+N+);
double ans = ;
for(int j = N; j > K; j--)
{
ans+=p[j];
}
return ans>=;
} int main()
{
while(~scanf("%d%d", &N, &K) && N|K)
{
for(int i = ; i <= N; i++)
{
scanf("%d", &a[i]);
}
for(int i = ; i <= N; i++)
{
scanf("%d", &b[i]);
}
double l = , r = ;
while(r-l>1e-) //!!!这里不用0,是因为计算机精度问题,计算浮点数要保留一定误差,改成0会超时
{
double mid = (l+r)/2.0;
if(ok(mid)) l = mid;
else r = mid;
}
printf("%.0lf\n", l*);
}
return ;
}
POJ 2976 Dropping tests 【01分数规划+二分】的更多相关文章
- POJ - 2976 Dropping tests(01分数规划---二分(最大化平均值))
题意:有n组ai和bi,要求去掉k组,使下式值最大. 分析: 1.此题是典型的01分数规划. 01分数规划:给定两个数组,a[i]表示选取i的可以得到的价值,b[i]表示选取i的代价.x[i]=1代表 ...
- POJ 2976 Dropping tests 01分数规划 模板
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6373 Accepted: 2198 ...
- POJ 2976 Dropping tests 01分数规划
给出n(n<=1000)个考试的成绩ai和满分bi,要求去掉k个考试成绩,使得剩下的∑ai/∑bi*100最大并输出. 典型的01分数规划 要使∑ai/∑bi最大,不妨设ans=∑ai/∑bi, ...
- $POJ$2976 $Dropping\ tests$ 01分数规划+贪心
正解:01分数规划 解题报告: 传送门! 板子题鸭,,, 显然考虑变成$a[i]-mid\cdot b[i]$,显然无脑贪心下得选出最大的$k$个然后判断是否大于0就好(,,,这么弱智真的算贪心嘛$T ...
- [poj 2976] Dropping tests (分数规划 二分)
原题: 传送门 题意: 给出n个a和b,让选出n-k个使得(sigma a[i])/(sigma b[i])最大 直接用分数规划.. code: //By Menteur_Hxy #include & ...
- POJ 2976 Dropping tests(分数规划)
http://poj.org/problem?id=2976 题意: 给出ai和bi,ai和bi是一一配对的,现在可以删除k对,使得的值最大. 思路: 分数规划题,可以参考<挑战程序竞赛> ...
- [poj2976]Dropping tests(01分数规划,转化为二分解决或Dinkelbach算法)
题意:有n场考试,给出每场答对的题数a和这场一共有几道题b,求去掉k场考试后,公式.的最大值 解题关键:01分数规划,double类型二分的写法(poj崩溃,未提交) 或者r-l<=1e-3(右 ...
- Dropping tests(01分数规划)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8176 Accepted: 2862 De ...
- POJ - 3111 K Best 0-1分数规划 二分
K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 12812 Accepted: 3290 Case Time ...
- POJ2976 Dropping tests —— 01分数规划 二分法
题目链接:http://poj.org/problem?id=2976 Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total S ...
随机推荐
- Unity GetComponentsInChildren<T>(true);
using System.Collections; using System.Collections.Generic; using UnityEngine; public class GetCompo ...
- (转)DB2 restart database命令的作用总结
DB2 restart database命令的作用总结 原文:https://blog.csdn.net/qingsong3333/article/details/62049039 信息中心对于RES ...
- 【原】shell编写一个简单的jmeter自动化压测脚本
在公司做压力测试也挺长时间了,每次测试前环境数据准备都需要话费较长时间,所以一直在考虑能不能将整个过程实现自动化进行,于是就抽空写了一个自动化脚本,当然这个脚本目前功能十分简陋,代码也不完善,很有很多 ...
- Aaja.pro 未定义
问题描述:安装新系统后,将代码迁至新系统,所有用到ajaxpro框架调用ajax方法时均报“xx未定义”的错: 解决问题的过程 : 1.看看你在前台调用的方法的命名空间,方法名和后台的是否对应.在后台 ...
- 动态网页开发jsp
1.动态网页的优势? ①交互性:即网页会根据用户的要求和选择而动态改变和显示内容. ③自动更新:即无需改变页面代码,便会自动生成新的页面内容. ④随机性:即当不同的时间.不同的人访问 ...
- Redis简介及持久化
Redis是一个key-value数据库,他不仅性能强劲,而且还具有复制特性以及为解决问题而生的独一无二的数据模型.可用于缓存.消息队列.发布.订阅消息.商品列表和评论列表等场景.Redis提供了5种 ...
- 抽象工厂模式&简单工厂模式
抽象工厂模式 优点: 如IFactory factory=new AccessFactory(),在一个应用中只需要初始化一次,这就使得改变应用的时候变得非常容易:其次它让具体的创建实例过程与客户端分 ...
- 使用durid的ConfigFilter对数据库密码加密
<!-- 配置dbcp数据源 --> <bean id="remoteDS" class="org.apache.commons.dbcp.BasicD ...
- CPU调度
概念 1.控制,协调进程对CPU的竞争,按一定的调度算法从就绪队列中选择一个进程把CPU的使用权交给被选中的进程, 如果没有就绪进程,系统会安排一个系统空闲进程或idle进程 cpu调度要解决的三个问 ...
- OLEDB 数据变更通知
除了之前介绍的接口,OLEDB还定义了其他一些支持回调的接口,可以异步操作OLEDB对象或者得到一些重要的事件通知,从而使应用程序有机会进行一些必要的处理.其中较有用的就是结果集对象的变更通知接口.通 ...