http://poj.org/problem?id=2976
通过二分 不断的移动左右边界 知道满足精度
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #define LL long long using namespace std; const double eps=1e-4; const int N=1005; double a[N]; double b[N]; double c[N]; bool cmp(double x,double y) { return x>y; } int main() { int n,k; while(scanf("%d %d",&n,&k)!=EOF) { if(n==0&&k==0) break; for(int i=0;i<n;++i) scanf("%lf",&a[i]); for(int i=0;i<n;++i) scanf("%lf",&b[i]); double l=0.0; double r=100.0; double mid; while(r-l>eps) { mid=(l+r)/2; for(int i=0;i<n;++i) c[i]=a[i]*100.0-b[i]*mid; sort(c,c+n,cmp); double sum=0; for(int i=0;i<n-k;++i) sum+=c[i]; if(sum>0.0) l=mid; else r=mid; } printf("%d\n",int(l+0.5)); } return 0; }