01分数规划POJ2976(简单模板题)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 7276 | Accepted: 2523 |
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 bifor 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个二元组(a,b),删除k个二元组,使得剩下的a元素之和与b元素之和的比率最大,最后的比率乘于100,然后输出跟最大比率最接近的整数
分析:设r=sigma(ai*xi)/sigma(bi*xi);其中xi={0,1},sigma(xi)=n-k,设R为最优值,
即:r<=R
即:sigma(ai*xi)/sigma(bi*xi)<=R
即:sigma(ai*xi)-sigma(R*bi*xi)<=0
也就是说sigma(ai*xi)-sigma(R*bi*xi)的最大值为0,
等价于 sigma((ai-R*bi)*xi)的最大值等于0;
因为h(r)=sigma((ai-R*bi)*xi)为单调递减函数,
所以可以二分求
#include"stdio.h"
#include"algorithm"
#include"string.h"
#include"iostream"
#include"queue"
#include"map"
#include"stack"
#include"cmath"
#include"vector"
#include"string"
#define M 1009
#define N 20003
#define eps 1e-7
#define mod 123456
#define inf 100000000
using namespace std;
int a[M],b[M],n,k;
double s[M];
int cmp(double a,double b)
{
return a>b;
}
double fun(int n,double r)
{
for(int i=1;i<=n;i++)
s[i]=a[i]-r*b[i];
sort(s+1,s+n+1,cmp);
double sum=0;
for(int i=1;i<=n-k;i++)
sum+=s[i];
return sum;
}
int main()
{
while(scanf("%d%d",&n,&k),n||k)
{
double l=0,r=0,mid=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
r+=a[i];
}
for(int i=1;i<=n;i++)
scanf("%d",&b[i]);
while(r-l>eps)
{
mid=(l+r)/2;
double msg=fun(n,mid);
if(msg<0)
{
r=mid;
}
else
{
l=mid;
}
}
printf("%.0lf\n",r*100);
}
return 0;
}
01分数规划POJ2976(简单模板题)的更多相关文章
- [JSOI 2016] 最佳团体(树形背包+01分数规划)
4753: [Jsoi2016]最佳团体 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2003 Solved: 790[Submit][Statu ...
- POJ3621Sightseeing Cows[01分数规划 spfa(dfs)负环 ]
Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9703 Accepted: 3299 ...
- BZOJ1486 HNOI2009 最小圈 【01分数规划】
BZOJ1486 HNOI2009 最小圈 Description 应该算是01分数规划的裸板题了吧..但是第一次写还是遇到了一些困难,vis数组不清零之类的 假设一个答案成立,那么一定可以找到一个环 ...
- BZOJ5090: [Lydsy1711月赛]组题(01分数规划)
5090: [Lydsy1711月赛]组题 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 785 Solved: 186[Submit][Status ...
- POJ 2976 Dropping tests 01分数规划 模板
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6373 Accepted: 2198 ...
- poj2976(01分数规划)
poj2976 题意 给出 a b 数组,一共 n 对数,其中最多可以去掉 k 对,问怎样使剩下比率(原始比率是 $ \frac{\sum_{i=1}^{n} a}{\sum_{i=1}^{n} b} ...
- Dropping tests [POJ2976] [01分数规划]
Description 今年有 n 场 ACM-ICPC 竞赛,小明每场都有资格参加.第 i 场竞赛共有 b[i] 道题.小明预测第 i场他能做出 a[i] 道题.为了让自己看着更“大佬”一些,小明想 ...
- [poj2976]Dropping tests(01分数规划,转化为二分解决或Dinkelbach算法)
题意:有n场考试,给出每场答对的题数a和这场一共有几道题b,求去掉k场考试后,公式.的最大值 解题关键:01分数规划,double类型二分的写法(poj崩溃,未提交) 或者r-l<=1e-3(右 ...
- poj2976(01分数规划)
poj2976 题意 给出 a b 数组,一共 n 对数,其中最多可以去掉 k 对,问怎样使剩下比率(原始比率是 $ \frac{\sum_{i=1}^{n} a}{\sum_{i=1}^{n} b} ...
随机推荐
- Delphi dbf文件如何定位记录(即设置Table的RecNum属性来移动到该记录号)
Delphi的table的RecNum属性,可以用来定位记录,如:self.Table1.RecNum:=23;即可以让数据库记录移动到23号记录上,但这种作用仅限于Paradox数据库,而不是dBA ...
- java BufferedWriter and BufferedReader
//Listing 5-2. Demonstrating the BufferedWriter and BufferedReader Classes import java.io.BufferedRe ...
- oj 1031 random permutation
Problem A: Random Permutations Time Limit: 1 Sec Memory Limit: 128 MB Submit: 91 Solved: 54 Descri ...
- 【译】Android系统架构
让我们来快速预览一下整个android系统的架构.从下面的图中我们可以发现,这个架构分为几个不同的层,底层为上一层提供服务. Linux Kernel android系统建立在一个坚固的基石上:Li ...
- 【转】用树莓派搭建web服务器
本文将详细介绍如何在树莓派上配置服务器,和<教你在Xubuntu上搭建LAMP服务器>有些类似,多了一些介绍在树莓派上的不同步骤的地方. 这种服务器的配置被称为LAMP,是最流行的服务器配 ...
- xdebug和xhprof
在安装时出现不是:1% 不是有效的win32 应用程序原因可能是是下载了64位的.dll扩展与当前的php不兼容
- appium testcase2
自己跑的两个case都在盘里,可以直接解压后放到workspace,加载工程就能跑,前提是你的环境没有问题 http://pan.baidu.com/s/1bnHCyn1 eclipse-File-i ...
- 【转】关于Android资源文件中出现百分号的问题
关于Android资源文件中出现百分号的问题 分类: Android JAVA2014-08-01 16:53 1345人阅读 评论(0) 收藏 举报 ANDROID格式化资源文件 目录(?)[+ ...
- 关于actionscript中新建一个sprite,设置大小(宽高)的问题。
有一定as3开发经验的童鞋应该知道,新建一个sprite,是无法设置大小的,即时设置了,也不会生效,宽高还是为0,据说反而有副作用(http://www.cnblogs.com/yjmyzz/arch ...
- Task多线程
Task多线程 1.首先是我们线程之间的启动. 1.这样 Task task = new Task(()=> { // System.Threading.Thread.Sleep(); Con ...