POJ 2976 (二分)
2016-03-16 16:54
197 查看
Dropping tests
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). Source Stanford Local 2005 |
需要从中选择n-k个.假设最终结果是x,二分枚举x,判断一种方案是否可行可以:
Σa[i]/Σb[i] = x
-----> Σa[i]-x*Σb[i]
= 0
因此可以对a[i]-x*b[i]从大到小排序选择前n-k个看是否满足>=0
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define maxn 1111
#define eps 1e-6
int n, k;
long long a[maxn], b[maxn];
double y[maxn];
bool ok (double x) {
for (int i = 0; i < n; i++)
y[i] = a[i]*1.0-x*b[i];
sort (y, y+n);
double ans = 0;
for (int i = 0; i < k; i++) {
ans += y[n-i-1];
}
return ans >= 0;
}
int main () {
//freopen ("in.txt", "r", stdin);
while (scanf ("%d%d", &n, &k) == 2) {
if (!n && !k)
break;
for (int i = 0; i < n; i++)
scanf ("%lld", &a[i]);
for (int i = 0; i < n; i++)
scanf ("%lld", &b[i]);
k = n-k;
double ans, l = 0, r = 1e15;
while (r-l > eps) {
double mid = (l+r)/2;
if (ok (mid))
l = mid;
else
r = mid;
}
if (ok (r))
ans = r;
else
ans = l;
ans *= 100;
printf ("%lld\n", (long long)(ans+0.5));
}
return 0;
}
相关文章推荐
- UI设计常用的图像格式及特点太原UI设计师郭文军分享给你
- python的PyPi源
- 学习Java的小结1
- Linux下常用压缩格式的压缩与解压方法
- Android仿微信小视频录制功能(二)
- 2、linux在后台运行一个命令
- 导航栏影响滚动视图的内部布局问题
- OpenLayers 学习(三)矢量图层上添加点绘制点、线、多边形
- redis主从配置
- 关于配置tnsnames来使用PLSQL连接数据库
- MyBatis报错
- 【转】你是不是也被Android Private Libraries、Referenced Libraries、android Dependency搞晕了~~
- 树莓派开发--内核编译与固件升级
- poj 3678 Katu Puzzle 2-SAT
- 重构Extract Method中关于从源函数提取目标函数的疑问
- 三个疑问与解决。
- 信息学奥林匹克竞赛-C语言输出3个数中最大值
- OKHttp介绍和实例展示
- MySQL Fabric高可用配置
- realloc,malloc,calloc三者的区别