原题链接
Dropping tests
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9915 Accepted: 3459
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
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).
Source
这个问题在《挑战程序设计竞赛》上有完全类似的问题就是求最大化平均值,这类问题可以归结为一类问题那就是有n个物品的重量和价值分别是wi和vi,从中选出k个物品使得单位重量的价值最大。
那么首先我们可以规定C(x)的意思为可以选择使得单位重量的价值不小于x。假设选了k个物品。那么必有这k个物品有如下图的关系
要得到这个k个值所以这个时候只需要把进行了v-x*w运算后的值进行从大到小的排序后选择前k个即可
//http://poj.org/problem?id=2976
#include <algorithm>
#include <iostream>
#include <utility>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const ll INFF = (~(0ULL)>>1);
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=1000 + 5;
struct node{int u,d;}a[maxn];
int n,k,MAX;
double shengyu[maxn];
//判断下当前的这个平均数是否可以
bool cmp(double x,double y){
return x>y;
}
bool can(double mid){
for(int i=0;i<n;i++)
shengyu[i]=a[i].u - mid*a[i].d;
sort(shengyu,shengyu+n,cmp);
//计算从大到小前k项的和
double sum=0;
for(int i=0;i<(n-k);i++)
sum+=shengyu[i];
return sum>=0;
}
void solve(){
double l=0,r=1;
for(int i=0;i<100;i++){
double mid=(l+r)/2;
//cout << "mid:" << mid << endl;
if(can(mid)) l=mid;
else r=mid;
}
printf("%d\n",(int)(r*100 + 0.5));
}
int main(){
while(scanf("%d%d",&n,&k)==2 && (n+k)){
//k=n-k;//把题目修改成选择k个科目使得平均分最大,并且这个时候最少还会选一门
for(int i=0;i<n;i++)
scanf("%d",&a[i].u);
for(int i=0;i<n;i++)
scanf("%d",&a[i].d);
solve();
}
return 0;
}