POJ 2976 Dropping tests(01分数规划入门)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11367 | Accepted: 3962 |
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
题目链接:POJ 2976
题意:给你N个物品,每一个物品有它的属性ai与bi,求丢掉K个物品后使留下来的N-K个物品的${\Sigma a_i} \over {\Sigma b_i}$最大化。
如果没听过01分数规划可以先看这篇文章:传送门1,传送门2,然后我想说的是其中对$r={{\Sigma a_i*x_i} \over {\Sigma b_i*x_i}}$的移项变形可以得到:$0={\Sigma a_i*x_i}-r*{\Sigma b_i*x_i}$
然后就是这里搞了一会儿才弄明白(数学渣没办法),想一想是不是很想熟悉的二次函数$y=ax^2+bx+c$的形式,有时候我们也利用$0=ax^2+bx+c$来推导二次函数,可以发现后者是前者的特殊情况,当y=0时前者便成了后者,或者说后者求出来的解是在自变量轴上的交点。那么上面那个函数同理,若把0改成F(r),则可以发现这是一个图像,由于bixi大于等于0,因此至少是关于r递减,又由于这个变量又受x_i集合的取值影响,这个图像实际上就像我们要使得$0={\Sigma a_i*x_i}-r*{\Sigma b_i*x_i}$成立,显然需要当$r=r_i$的时候$F(r)=0$,但是这样的取值$r_i$可以有很多个,那么我们要找到最靠右的那个点作为答案即可。当然这题还用到了贪心的思想,因为取哪个是随意的,当然取每一次取最优的
代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1010;
const double eps = 1e-5;
double a[N], b[N], d[N]; int main(void)
{
int n, k, i;
while (~scanf("%d%d", &n, &k) && (n | k))
{
for (i = 0; i < n; ++i)
scanf("%lf", a + i);
for (i = 0; i < n; ++i)
scanf("%lf", b + i);
double L = 0, R = 1e9 + 7, ans = 0;
int res = n - k;
while (fabs(R - L) >= eps)
{
double mid = (L + R) / 2.0;
for (i = 0; i < n; ++i)
d[i] = a[i] - mid * b[i];
sort(d, d + n, greater<double>());
double temp = 0;
for (i = 0; i < res; ++i)
temp += d[i];
if (temp > 0)
{
L = mid;
ans = mid;
}
else
R = mid;
}
printf("%.0f\n", 100.0 * ans);
}
return 0;
}
POJ 2976 Dropping tests(01分数规划入门)的更多相关文章
- 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(01分数规划---二分(最大化平均值))
题意:有n组ai和bi,要求去掉k组,使下式值最大. 分析: 1.此题是典型的01分数规划. 01分数规划:给定两个数组,a[i]表示选取i的可以得到的价值,b[i]表示选取i的代价.x[i]=1代表 ...
- POJ 2976 Dropping tests(分数规划)
http://poj.org/problem?id=2976 题意: 给出ai和bi,ai和bi是一一配对的,现在可以删除k对,使得的值最大. 思路: 分数规划题,可以参考<挑战程序竞赛> ...
- [poj 2976] Dropping tests (分数规划 二分)
原题: 传送门 题意: 给出n个a和b,让选出n-k个使得(sigma a[i])/(sigma b[i])最大 直接用分数规划.. code: //By Menteur_Hxy #include & ...
- Dropping tests(01分数规划)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8176 Accepted: 2862 De ...
- [poj2976]Dropping tests(01分数规划,转化为二分解决或Dinkelbach算法)
题意:有n场考试,给出每场答对的题数a和这场一共有几道题b,求去掉k场考试后,公式.的最大值 解题关键:01分数规划,double类型二分的写法(poj崩溃,未提交) 或者r-l<=1e-3(右 ...
- POJ2976 Dropping tests —— 01分数规划 二分法
题目链接:http://poj.org/problem?id=2976 Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total S ...
- poj Dropping tests 01分数规划---Dinkelbach算法
果然比二分要快将近一倍.63MS.二分94MS. #include <iostream> #include <algorithm> #include <cstdio> ...
随机推荐
- 动态规划专题(三)——数位DP
前言 数位\(DP\) 真的是最恶心的\(DP\). 简介 看到那种给你两个数,让你求这两个数之间符合条件的数的个数,且这两个数非常大,这样的题目一般就是 数位\(DP\) 题. 数位\(DP\)一般 ...
- 索引属性 unique指定
比较重要的属性有: 名字 db.collection.ensureIndex({},{name:''}) 在创建索引时,mongodb会自己给索引创建默认的名字,这种名字并不好记,我们看一下mongo ...
- 【转】iOS开发4:关闭键盘
在 iOS 程序中当想要在文本框中输入数据,轻触文本框会打开键盘.对于 iPad 程序,其键盘有一个按钮可以用来关闭键盘,但是 iPhone 程序中的键盘却没有这样的按钮,不过我们可以采取一些方法关闭 ...
- java常用 开源
http://sourceforge.nethttp://code.google.com/hosting/http://www.open-open.com/code/tags/Javahttp://w ...
- C# StreamReader对象
1.读取文件 输入流用于从外部源读取数据,在很多情况下,数据源可以是磁盘上的文件或网络的某些位置,任何可能发送数据的位置都可以是数据源,比如网络应用程序,web服务,甚至是控制台.StreamRead ...
- java 程序设计第三次作业内容
第一题:输出结果是什么? System.out.println("5+5="+5+5); 第二题:输出结果是什么? int a=3,b; b=a++; sop("a=&q ...
- display :inline-block 处理点小障碍
使用inline-block之前先处理点小障碍:inline-block元素会有4px左右的空隙,这个是因为我们写代码时候的换行符所致. 解决办法很简单:在inline-block的父元素中设置样式f ...
- 二十三、MySQL 事务
MySQL 事务 MySQL 事务主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数 ...
- 浅谈JavaScript字符串拼接
本文给大家汇总介绍了几种javascript中字符串拼接的方法,十分的简单实用,有需要的小伙伴可以参考下. 在JavaScript中会经常遇到字符串拼接,但是如果要拼接的字符串过长就比较麻烦了. 如果 ...
- Laravel 命令行常用命令
一.简介 1.Artisan 是 Laravel 自带的命令行接口名称,它为我们在开发过程中提供了很多有用的命令.想要查看所有可用的Artisan命令,可使用list命令: php artisan l ...