POJ2976 Dropping tests 01分数规划

二分答案ans,可让精度恶心死我了

找到使a[i]-l*b[i]最大的l。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 const double eps = 1e-7;
 7 
 8 int n,k;
 9 double ans,a[1010],b[1010],d[1010];
10 
11 int main(){
12     while(true){
13         cin >> n >> k;
14         if(n == 0&&k == 0)break;
15         for(int i = 1;i <= n;i++)scanf("%lf",&a[i]);
16         for(int i = 1;i <= n;i++)scanf("%lf",&b[i]);
17         double l = 0.0,r = 1.0,mid;
18         for(int i = 1;i <= 300;i++){
19             mid = (l+r)/2;
20             for(int i = 1;i <= n;i++)d[i] = a[i]-mid*b[i];
21             sort(d+1,d+n+1); 
22             double sum = 0.0;
23             for(int i = k+1;i <= n;i++)sum += d[i];
24             if(sum >= -eps)ans = mid,l = mid;
25             else r = mid;
26         }
27         printf("%.0f\n",ans*100);
28     }    
29 return 0;
30 }

 

posted @ 2019-11-03 15:00  TIH_HIT  阅读(82)  评论(0编辑  收藏  举报