POJ 2976 Dropping tests【二分答案+分数规划】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37867156/article/details/81584295

Dropping tests

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18430   Accepted: 6376

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).

参考:https://blog.csdn.net/hzoi_ztx/article/details/54898323

https://www.cnblogs.com/proverbs/archive/2013/01/09/2853725.html

设:xi∈{0,1},表示第i个二元组是否留下,p为比率,P为p的最大值,即比例的最大值(注意区分大P和小p)

则:p=sigma(ai*xi)/sigma(bi*xi)   其中sigma(xi)=n-k

显然:对于所有可能的取得的 p 的值,p<=P

即:对于所有可能的xi的组合,sigma(ai*xi)/sigma(bi*xi)<=P

即:sigma(ai*xi)/sigma(bi*xi)的最大值等于P

即:sigma(ai*xi)-sigma(P*bi*xi)<=0,也就是sigma(ai*xi)-sigma(P*bi*xi)的最大值等于0

由于:sigma(ai*xi)-sigma(p*bi*xi)等价于sigma((ai-bi*p)*xi),sigma((ai-bi*P)*xi)的最大值就等于0

同理:

设:我们枚举的p的最大值为m(对于所有的xi的组合,不一定存在m这个取值)

若m>P,sigma((ai-bi*P)*xi)的最大值小于0

若m<P,sigma((ai-bi*P)*xi)的最大值大于0

然后通过二分逼近的方法可以求出m=P~

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int maxn=1005;
int n,k;
int a[maxn],b[maxn];
double l,r,d[maxn];

int main(){
	while(~scanf("%d%d",&n,&k)&&(n+k))	{
		double ans=0;
		l=0,r=1;
		for(int i=0;i<n;i++)
			scanf("%d",&a[i]);
		for(int i=0;i<n;i++)
			scanf("%d",&b[i]);
		double mid;
		while(r-l>1e-8){
			ans=0;
			mid=(l+r)/2;
			for(int i=0;i<n;i++)
				d[i]=a[i]-mid*b[i];
			sort(d,d+n);
			for(int i=k;i<n;i++){
				ans+=d[i];
			}
			if(ans>0) l=mid;
			else r=mid;
		}
		printf("%.0lf\n",mid*100);
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37867156/article/details/81584295