Dropping tests
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(简单模板题)的更多相关文章

  1. [JSOI 2016] 最佳团体(树形背包+01分数规划)

    4753: [Jsoi2016]最佳团体 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2003  Solved: 790[Submit][Statu ...

  2. POJ3621Sightseeing Cows[01分数规划 spfa(dfs)负环 ]

    Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9703   Accepted: 3299 ...

  3. BZOJ1486 HNOI2009 最小圈 【01分数规划】

    BZOJ1486 HNOI2009 最小圈 Description 应该算是01分数规划的裸板题了吧..但是第一次写还是遇到了一些困难,vis数组不清零之类的 假设一个答案成立,那么一定可以找到一个环 ...

  4. BZOJ5090: [Lydsy1711月赛]组题(01分数规划)

    5090: [Lydsy1711月赛]组题 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 785  Solved: 186[Submit][Status ...

  5. POJ 2976 Dropping tests 01分数规划 模板

    Dropping tests   Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6373   Accepted: 2198 ...

  6. poj2976(01分数规划)

    poj2976 题意 给出 a b 数组,一共 n 对数,其中最多可以去掉 k 对,问怎样使剩下比率(原始比率是 $ \frac{\sum_{i=1}^{n} a}{\sum_{i=1}^{n} b} ...

  7. Dropping tests [POJ2976] [01分数规划]

    Description 今年有 n 场 ACM-ICPC 竞赛,小明每场都有资格参加.第 i 场竞赛共有 b[i] 道题.小明预测第 i场他能做出 a[i] 道题.为了让自己看着更“大佬”一些,小明想 ...

  8. [poj2976]Dropping tests(01分数规划,转化为二分解决或Dinkelbach算法)

    题意:有n场考试,给出每场答对的题数a和这场一共有几道题b,求去掉k场考试后,公式.的最大值 解题关键:01分数规划,double类型二分的写法(poj崩溃,未提交) 或者r-l<=1e-3(右 ...

  9. poj2976(01分数规划)

    poj2976 题意 给出 a b 数组,一共 n 对数,其中最多可以去掉 k 对,问怎样使剩下比率(原始比率是 $ \frac{\sum_{i=1}^{n} a}{\sum_{i=1}^{n} b} ...

随机推荐

  1. instruction-set architecture Processor Architecture

    Computer Systems A Programmer's Perspective Second Edition We have seen that a processor must execut ...

  2. 原生js实现跑马灯抽奖效果

    目前好多的微信活动都有一些抽奖活动,其中就有跑马灯. <!DOCTYPE html> <html> <head> <title>跑马灯效果</ti ...

  3. 取url的键值对,location的search:从?开始的字符串

    function urlArgs(){ var args=""; var query=location.search.substring(1);//去除问号 var pairs=q ...

  4. 第四章 跨平台图像显示库——SDL 第一节 与SDL第一次亲密接触

    http://blog.csdn.net/visioncat/article/details/1596576 GCC for Win32 开发环境介绍(5) 第四章 跨平台图像显示库——SDL 第一节 ...

  5. 关于java字符串编译优化问题

    情景一:不好的字符串拼接习惯    起因是这样的:一个大牛在写了一篇关于java字符串优化问题的讲解,他提到:不要使用strObj+otherValue的方法将otherValue转换为字符串形式,因 ...

  6. hiveserver2以及beeline客户端的使用

    一:开启服务 1.启动前端的hiveserver2 按住ctrl+c就可以结束这个服务. 2.怎么知道已经开启的服务 如果进程在后台,可以查出来,kill这些进程. 3.后端开启服务 二:beelin ...

  7. HDFS API 文件读写代码演示

    一:准备工作 1.新建class类 2.开启HDFS服务 3.将配置文件拷贝进resources路径 方便了Configuration的读取配置. 二:读出HDFS文件系统中的文件到控制台 4.读出在 ...

  8. Erlang ERTS的Trap机制的设计及其用途

    出处:http://mryufeng.iteye.com/blog/334744 erlang的trap机制在实现中用的很多,在费时的BIF操作中基本上都可以看到.它的实现需要erl vm的配合.它的 ...

  9. 【Android测试】【第七节】Monkey——源码浅谈

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4713466.html 前言 根据上一篇我们学会了Monke ...

  10. 让Dreamweaver支持less

    编辑->首选参数->文件类型/编辑器->在代码视图中打开->添加" .less"后缀