[poj 2976]Dropping tests 01分数规划

Dropping tests
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5992   Accepted: 2076

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
 
 

题目大意

手中有A场考试,每场考试答对的题目数为a[i],题目数为b[i],可以不计其中m场的结果,求答对题数/总题数的最大值

解题思路

01分数规划入门,算法来自http://www.cnblogs.com/perseawe/archive/2012/05/03/01fsgh.html

这个时候我们只需设d[i]=a[i]-b[i]*x,对于每一个x求sum{d[i]}是否大于0。

因为最优(最大)可行解只有一个,二分求解。

每次判断时只需要拿出d[i]最大的n-m个值即可。

code:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> 
#include <string>
#define eps 1e-9
using namespace std;
int cmp(double x,double y)
{
	return x>y;
}
double a[1005];
double b[1005];
double d[1005];
int n,m,k;
int main()
{
	while(~scanf("%d%d",&n,&m),n+m)
	{
		for (int i=1;i<=n;i++)
			scanf("%lf",&a[i]);
		for (int i=1;i<=n;i++)
			scanf("%lf",&b[i]);
		double ma=1,mi=0;
		while (ma-mi>eps)
		{
			double mid=(ma+mi)/2;
			for (int i=1;i<=n;i++)
				d[i]=a[i]-mid*b[i];
			sort(d+1,d+1+n,cmp);
			double check=0;
			for (int i=1;i<=n-m;i++)
				check+=d[i];
			if (check>=0) mi=mid;
			else ma=mid;
		}
		printf("%.0f\n",ma*100);
	}
	return 0;
}


你可能感兴趣的:(数学)