POJ:2976-Dropping tests(二分平均值)
Dropping tests
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15508 Accepted: 5418
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 100*sum(ai)/sum(bi)
.
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).
中文题意:
今年有 n 场 ACM-ICPC 竞赛,小明每场都有资格参加。第 i 场竞赛共有 b[i] 道题。小明预测第 i 场他能做出 a[i] 道题。为了让自己看着更“大佬”一些,小明想让自己平均做出的题数越大越好,也就是最大化大佬度,大佬度的定义如下:
为了达到这个目的,小明决定放弃 k 场比赛的参赛资格。请求出最大的大佬度。
例如有 3 场小型比赛,题数分别是 5 题、1 题、6 题,小明预测自己分别能做出 5 题、0题、2题。如果每场都参加,那么大佬度是 ,看着不怎么大佬。不过,如果放弃第 3 场比赛,那么大佬度就是 ,看着更加大佬了。
Input
输入测试文件含有多组测试,每组有 3 行。第一行有 2 个整数, 1 ≤ n ≤ 1000 和 0 ≤ k < n。第二行有 n 个整数,即每个 a[i]。第三行含有 n 个正整数 b[i]。保证 0 ≤ a[i] ≤ b[i] ≤ 1, 000, 000, 000。文件末尾由 n = k = 0 标识,并且不应该被处理。
Output
对于每组测试数据,输出一行整数,即放弃 k 场比赛后可能的最高大佬度。大佬度应该舍入到最近的整数。
解题心得:
- 其实就是一个最大化平均值的问题,最大化平均值二分的一种经典应用,先假设答案为ans,如果100 * sum(ai) / sum(bi) > ans,那么按照这种大于/小于的关系式来二分答案,但是在运算的时候需要转化一下关系式,可以转换成:100 * sum(ai) > sum(bi) * ans,按照乘法的分配律可以写成100 * sum(ai) > sum(bi * ans),然后移项得到关系式:100 * sum(ai) - sum(bi * ans) > 0,然后按照减法的分配律可以写成sum(100 * ai - bi * ans) > 0,这样可以先得出100 * ai-bi * ans,然后降序排序,算出前n - k个是否大于0,然后以此二分就行了。
#include <algorithm>
#include <cstring>
#include <stdio.h>
using namespace std;
typedef long long ll;
const int maxn = 1010;
ll n,k,a[maxn],b[maxn];
double c[maxn];
void init() {
for(int i=0;i<n;i++)
scanf("%lld",&a[i]);
for(int i=0;i<n;i++)
scanf("%lld",&b[i]);
}
bool cmp(double a,double b) {
return a > b;
}
bool checke(double va) {
for(ll i=0;i<n;i++)
c[i] = (double)a[i]*100.0 - va*b[i];
sort(c,c+n,cmp);
double sum = 0;
for(int i=0;i<n-k;i++)
sum += c[i];
return sum >= 0;
}
double binary_search() {
double l,r;
l = 0, r = 1e18+100;
for(int i=0;i<100;i++) {//为了保证精度问题直接循环100次
double mid = (l + r) / 2.0;
if(checke(mid))
l = mid;
else
r = mid;
}
return l;
}
int main() {
while(scanf("%lld%lld",&n,&k) && n|k) {
init();
double ans = binary_search();
ll int_ans = (ans + 0.5);
printf("%lld\n",int_ans);
}
return 0;
}
POJ:2976-Dropping tests(二分平均值)的更多相关文章
- POJ 2976 Dropping tests [二分]
1.题意:同poj3111,给出一组N个有价值a,重量b的物品,问去除K个之后,剩下的物品的平均值最大能取到多少? 2.分析:二分平均值,注意是去除K个,也就是选取N-K个 3.代码: # inclu ...
- poj 2976 Dropping tests (最大化平均值:二分查找)
#include<iostream> #include<algorithm> #include<stdio.h> #include<math.h> #d ...
- POJ 2976 Dropping tests (二分+贪心)
题意:给定 n 个分数,然后让你去年 m 个分数,使得把剩下的所有的分子和分母都相加的分数最大. 析:这个题并不是分子越大最后结果就越大,也不是整个分数越大,最后结果就越大的,我们可以反过来理解,要去 ...
- 二分算法的应用——最大化平均值 POJ 2976 Dropping tests
最大化平均值 有n个物品的重量和价值分别wi 和 vi.从中选出 k 个物品使得 单位重量 的价值最大. 限制条件: <= k <= n <= ^ <= w_i <= v ...
- POJ - 2976 Dropping tests && 0/1 分数规划
POJ - 2976 Dropping tests 你有 \(n\) 次考试成绩, 定义考试平均成绩为 \[\frac{\sum_{i = 1}^{n} a_{i}}{\sum_{i = 1}^{n} ...
- POJ 2976 Dropping tests 【01分数规划+二分】
题目链接:http://poj.org/problem?id=2976 Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ 2976 Dropping tests 01分数规划 模板
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6373 Accepted: 2198 ...
- POJ 2976 Dropping tests(01分数规划)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions:17069 Accepted: 5925 De ...
- POJ 2976 Dropping tests(01分数规划入门)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11367 Accepted: 3962 D ...
- POJ 2976 Dropping tests (0/1分数规划)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4654 Accepted: 1587 De ...
随机推荐
- CSS列表(新闻列表、导航条)常见写法
以下面这个UL做演示 <ul> <li><a href="#"><span>2014-4-1</span>教育</ ...
- Python is 和 == 的区别, 编码和解码
一.is 和 == 的区别 is : 进行比较,比较的是内存地址是否一致 ==:进行比较,比较的是值是否相等 1.小数据池: 数字小数据池范围 -5~256 字符串中如果有特殊字符则他们的内存地址不一 ...
- [SVN]TortoiseSVN工具培训1─为什么要用SVN?
你是否有经历过以下情况: SVN的使用理由/好处:
- SharePoint 栏的三种名字Filed :StaticName、 InternalName、 DisplayName
SharePoint 的栏,有3个名字, StaticName InternalName DisplayName. 当在第一次创建栏的时候,这3个名字一起进行创建,并且都一样. <FIELD ...
- mysql:JDBC url 参数详解
MySql链接url参数详解 jdbc:mysql://[host:port],[host:port].../[database][?参数名1][=参数值1][&参数名2][=参数值2]... ...
- 如何在SecureCRT中给linux上传和下载文件
方法/步骤 需要上传或者下载,需要使用rz和sz命令.如果linux上没有这两个命令工具,则需要先安装.可以使用yum安装.运行命令yum install lrzsz. 安装完成后就可以使 ...
- 概念:RPG游戏中两个兵种互相攻击的逻辑
直接上题目: 解析题目: 根据题目的解析,进行代码的实现: 输出结果: 心得: (1) 当我们面对‘公式结果不是我们想要的’时,应该在脑海里将一个完整的攻击流程进行想象,就会对流程有个更清晰的思路 ( ...
- Selenium入门16 获取页面源代码
页面源代码:page_source属性 获取源代码之后,再用正则表达式匹配出所有的链接,代码如下: #coding:utf-8 from selenium import webdriver impor ...
- ZOJ - 2112 Dynamic Rankings(BIT套主席树)
纠结了好久的一道题,以前是用线段树套平衡树二分做的,感觉时间复杂度和分块差不多了... 终于用BIT套函数式线段树了过了,120ms就是快,此题主要是卡内存. 假设离散后有ns个不同的值,递归层数是l ...
- BZOJ 3233: [Ahoi2013]找硬币
BZOJ 3233: [Ahoi2013]找硬币 标签(空格分隔): OI-BZOJ OI-DP Time Limit: 10 Sec Memory Limit: 64 MB Description ...