Dropping tests
题目链接:http://poj.org/problem?id=2976
| 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=+;
const double inf=1e15;
const double eps=1e-;
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=;i<N;i++)//找到最优的解
{
c[i]=a[i]-mid*b[i];
}
sort(c,c+N,cmp);
double sum=;
for(int i=;i<N-K;i++) sum+=c[i];
return sum>=;
}
int main()
{
while(cin>>N>>K)
{
if(N==&&K==) break;
for(int i=;i<N;i++) cin>>a[i];
for(int i=;i<N;i++) cin>>b[i];
double l=,r=inf;
double ans; while(r-l>=eps)
{
double mid=(r+l)/;
//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 * ); /*
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 ;
} /*
*/
ac的:
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
typedef long long ll;
const int maxn=+;
const double inf=1e15;
const double eps=1e-;
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=;i<N;i++)//找到最优的解
{
c[i]=a[i]-mid*b[i];
}
sort(c,c+N,cmp);
double sum=;
for(int i=;i<N-K;i++) sum+=c[i];
return sum>=;
}
int main()
{
while(cin>>N>>K)
{
if(N==&&K==) break;
for(int i=;i<N;i++) cin>>a[i];
for(int i=;i<N;i++) cin>>b[i];
double l=,r=inf;
double ans; while(r-l>=eps)
{
double mid=(r+l)/;
//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 * ); /*
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 ;
} /*
*/
Dropping tests的更多相关文章
- POJ2976 Dropping tests(二分+精度问题)
---恢复内容开始--- POJ2976 Dropping tests 这个题就是大白P144页的一个变形,二分枚举x,对a[i]-x*b[i]从大到小进行排序,选取前n-k个判断和是否大于等于0,若 ...
- Dropping tests(01分数规划)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8176 Accepted: 2862 De ...
- 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 P2976] Dropping tests
[poj P2976] Dropping tests Time Limit: 1000MS Memory Limit: 65536K Description In a certain course, ...
- POJ 2976 Dropping tests (0/1分数规划)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4654 Accepted: 1587 De ...
- HDU2976 Dropping tests 2017-05-11 18:10 39人阅读 评论(0) 收藏
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12187 Accepted: 4257 D ...
- 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
最大化平均值 有n个物品的重量和价值分别wi 和 vi.从中选出 k 个物品使得 单位重量 的价值最大. 限制条件: <= k <= n <= ^ <= w_i <= v ...
- 【POJ2976】Dropping Tests(分数规划)
[POJ2976]Dropping Tests(分数规划) 题面 Vjudge 翻译在\(Vjudge\)上有(而且很皮) 题解 简单的\(01\)分数规划 需要我们做的是最大化\(\frac{\su ...
随机推荐
- 3.3PCL已有点类型介绍和增加自定义的点类型
1.PCL中有哪些可用的PointT类型 这些point类型都位于point_types.hpp文件中,如果用户需要自己定义类型,需要对已有类型了解. 1)PointXYZ---成员变量:float ...
- 【Java学习】Java迭代器
迭代器是一种模式,它可以使得对于序列类型的数据结构的遍历行为与被遍历的对象分离,即我们无需关心该序列的底层结构是什么样子的.只要拿到这个对象,使用迭代器就可以遍历这个对象的内部. 1.Iterator ...
- MacBook Pro (13 英寸, 2017 年)安装win10系统
准备: windows10系统镜像 16G或更大容量U盘(存放驱动,必须格式化为FAT) 官方提供的驱动软件 详细步骤: 1.打开Bootcamp,选择镜像文件 2.点击下一步,选择Bootcamp分 ...
- 「十二省联考 2019」异或粽子——tire树+堆
题目 [题目描述] 小粽是一个喜欢吃粽子的好孩子.今天她在家里自己做起了粽子. 小粽面前有 $n$ 种互不相同的粽子馅儿,小粽将它们摆放为了一排,并从左至右编号为 $1$ 到 $n$.第 $i$ 种馅 ...
- dedecms有缩略图则显示缩略图,没有则显示随机缩略图
随着html5以及扁平化等新的设计概念的深入人心,缩略图功能则成了一般网页模版制作不可或缺的一个功能,dedecms默认的的缩略图调用标签[field:imglink/] 或者 [field:litp ...
- P3380 【模板】二逼平衡树(树套树) 线段树套平衡树
\(\color{#0066ff}{ 题目描述 }\) 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 查询k在区间内的排名 查询区间内排名为k的值 修改某一位值上 ...
- EXPEDI - Expedition 优先队列
题目描述 A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rathe ...
- Linux系统centos中sudo命令不能用----提升权限
gyx is not in the sudoers file. This incident will be reported. 1.切换到root用户 su ,如果想要切换回去 exit 2.添加s ...
- HashMap 1.8的源码分析一
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, ...
- How to pass multiple parameters in PowerShell invoke-restmethod
Link: http://www.tagwith.com/question_322855_how-to-pass-parameters-in-powershell-invoke-restmethod- ...