Dropping tests
64-bit integer IO format: %lld Java class name: Main
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 nintegers 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
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const double exps = 1e-6; 18 struct node{ 19 double c,a,b; 20 }; 21 node p[1010]; 22 int n,k; 23 bool cmp(const node &x,const node &y){ 24 return x.c < y.c; 25 } 26 double test(){ 27 double ans,x,y,tmp = 0; 28 while(true){ 29 ans = tmp; 30 for(int i = 0; i < n; i++) 31 p[i].c = p[i].a - ans*p[i].b; 32 sort(p,p+n,cmp); 33 x = y = 0; 34 for(int i = k; i < n; i++){ 35 x += p[i].a; 36 y += p[i].b; 37 } 38 tmp = x*1.0/y; 39 if(fabs(tmp - ans) < exps) return ans; 40 } 41 } 42 int main() { 43 while(scanf("%d %d",&n,&k),n||k){ 44 for(int i = 0; i < n; i++) 45 scanf("%lf",&p[i].a); 46 for(int i = 0; i < n; i++) 47 scanf("%lf",&p[i].b); 48 printf("%.0f\n",test()*100); 49 } 50 return 0; 51 }