POJ——T 2976 Dropping tests

http://poj.org/problem?id=2976

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13861   Accepted: 4855

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 npositive 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个物品,每个物品有两个属性a和b,选择n-k个元素,询问sum{ai}/sum{bi}的最大值。
分数规划 二分答案ans , 判断 sum[a[i]]/sum[b[i]]与ans的关系
  即 判断 b[i]*ans-a[i]*100+b[i+1]*ans-a[i+1]*100+...+b[i+k]*ans-a[i+k]*100<0
 1 #include <algorithm>
 2 #include <cstdio>
 3 
 4 inline void read(int &x)
 5 {
 6     x=0; register char ch=getchar();
 7     for(; ch>'9'||ch<'0'; ) ch=getchar();
 8     for(; ch>='0'&&ch<='9'; ch=getchar()) x=x*10+ch-'0';
 9 }
10 const double eps(1e-9);
11 const int N(1005);
12 double a[N],b[N];
13 int n,k;
14 
15 double l,r,mid,ans,tmp[N];
16 inline bool check(double x)
17 {
18     double sum=0.0;
19     for(int i=1; i<=n; ++i)
20         tmp[i]=1.0*b[i]*x-100.0*a[i];
21     std::sort(tmp+1,tmp+n+1);
22     for(int i=1; i<=n-k; ++i) sum+=tmp[i];
23     return sum<0;
24 }
25 
26 int Presist()
27 {
28     for(; 1; )
29     {
30         read(n),read(k);        if(!n&&!k) break;
31         for(int i=1; i<=n; ++i) scanf("%lf",&a[i]);
32         for(int i=1; i<=n; ++i) scanf("%lf",&b[i]);
33         for(l=0,r=100.0; r-l>eps; )
34         {
35             mid=(l+r)/2.0;
36             if(check(mid))
37                  l=mid;
38             else r=mid;
39         }
40         printf("%.0lf\n",l);
41     }
42     return 0;
43 }
44 
45 int Aptal=Presist();
46 int main(int argc,char**argv){;}

 

posted @ 2017-10-14 20:42  Aptal丶  阅读(218)  评论(0编辑  收藏  举报