POJ-2976 Dropping tests 01分数规划
题目链接:http://poj.org/problem?id=2976
很典型的01分数规划,sort+二分即可。注意精度问题,这种四舍五入的问题一般都是两种处理方法:1、printf("%.0lf\n",ans); 2、printf("%d\n",(int)(ans+0.5));
1 //STATUS:C++_AC_47MS_196KB 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<string.h> 5 #include<math.h> 6 #include<iostream> 7 #include<string> 8 #include<algorithm> 9 #include<vector> 10 #include<queue> 11 #include<stack> 12 using namespace std; 13 #define LL __int64 14 #define pii pair<int,int> 15 #define Max(a,b) ((a)>(b)?(a):(b)) 16 #define Min(a,b) ((a)<(b)?(a):(b)) 17 #define mem(a,b) memset(a,b,sizeof(a)) 18 #define lson l,mid,rt<<1 19 #define rson mid+1,r,rt<<1|1 20 const int N=1010,M=1000000,INF=0x3f3f3f3f,MOD=1999997; 21 const LL LLNF=0x3f3f3f3f3f3f3f3fLL; 22 const double DNF=100000000; 23 24 double d[N]; 25 int a[N],b[N]; 26 int n,k; 27 28 double binary() 29 { 30 int i; 31 double l=0,r=1,mid,s; 32 while(r-l>1e-4){ 33 mid=l+(r-l)/2; 34 for(i=0;i<n;i++)d[i]=a[i]-mid*b[i]; 35 sort(d,d+n); 36 for(s=0,i=n-1;i>=k;i--)s+=d[i]; 37 if(s>0)l=mid; 38 else r=mid; 39 } 40 return mid; 41 } 42 43 int main() 44 { 45 // freopen("in.txt","r",stdin); 46 int i,j; 47 while(~scanf("%d%d",&n,&k) && (n||k)) 48 { 49 for(i=0;i<n;i++)scanf("%d",a+i); 50 for(i=0;i<n;i++)scanf("%d",b+i); 51 52 printf("%d\n",(int)(binary()*100+0.5)); 53 } 54 return 0; 55 }