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. ifarm 子 父页面方法如何互调

    1.iframe子页面调用父页面js函数 子页面调用父页面函数只需要写上window.praent就可以了.比如调用a()函数,就写成: 代码如下: window.parent.a(); 子页面取父页 ...

  2. Delphi中如何实现滚动文字

    1.先添加一个Timer控件,其Interval属性设置为50. 2.再添加一个Label控件,Name为Label1. 3.然后在Timer的OnTimer事件添加如下代码: unit Unit13 ...

  3. Java 获取类名,函数名,行数

    C++下用宏来实现.分别是__FILE__,__func__,__LINE__分别代表,C++编译自动在每个文件中设定__FILE__类型是字符串常量 ,将__LINE__替换为当前行数,类型是数字常 ...

  4. 【转】Unity3D开发之Http协议网络通信

    之前unity3d项目要做跟服务器通信的模块,然后服务器那边的协议是基于http的Jsonrpc通信方式一开始,用C#的本身类HttpWebRequest来提交请求,很快就在电脑上面成功了,代码也很简 ...

  5. C# Unicode编码与解码方法

    public static class ExtentMethod { public static string ToUnicodeString(this string str) { StringBui ...

  6. C/C++ 中长度为0的数组

    参考文献:http://blog.csdn.net/zhaqiwen/article/details/7904515 近日在看项目中的框架代码时,发现了了一个奇特的语法:长度为0的数组例如 uint8 ...

  7. php--城市分类

    效果图:

  8. 创建podSpec,使用pod管理第三方库

    提要: podfile文件会先读取.podspec文件,根据.podspec文件的指向来下载第三方库到项目中. 本文先通过一.二.三项,这三个步骤讲解了如何建立一个.podspec文件在本地.coco ...

  9. [LeetCode] Subsets (bfs的vector实现)

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  10. iOS 的 XMPPFramework 简介

    XMPPFramework是一个OS X/iOS平台的开源项目,使用Objective-C实现了XMPP协议(RFC-3920),同时还提供了用于读写XML的工具,大大简化了基于XMPP的通信应用的开 ...