poj 2976 分数规划
题目要去掉一些的a[i],b[i]对,使得sigma(a[i])/sigma(b[i])最大,
设100*sigma(a[i])/sigma(b[i])=r 最大,则 100*sigma(a[i])-sigma(b[i])*r=0时r最大。
因为这个式子是单调的,所以二分枚举答案,sigma(100*ai-mid*bi)==0时就为答案
View Code
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; struct node{ double a,b,c; }in[1010]; int cmp(node a,node b){ return a.c>b.c; } int main() { int i,j,n,k; while(scanf("%d%d",&n,&k),(n||k)) { for(i=1;i<=n;i++) scanf("%lf",&in[i].a); for(j=1;j<=n;j++) scanf("%lf",&in[j].b); double ans=0,l=0,r=100.0,mid; while(r-l>1e-4) { mid=(l+r)/2; for(i=1;i<=n;i++) in[i].c=100.0*in[i].a-mid*in[i].b; sort(in+1,in+n+1,cmp); double sum=0; for(i=1;i<=n-k;i++) sum+=in[i].c; if(sum>0) l=mid; else r=mid; } printf("%.0lf\n",mid); } }