AcWing 234. 放弃测试
考察:01分数规划
思路:
由奶牛观光所说,01分数规划基本是在考察二分与其他算法的结合.这里也可以化为累加 a[i] - mid*b[i] 注意原式子*100,所以mid需要/100
我们用C数组保存a[i] - mid*b[i]的值,然后选出最大的n-k个,如果和>=0就说明正确.
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 typedef long long LL; 6 const double eps = 1e-6; 7 const int N = 1010; 8 int n,k,a[N],b[N]; 9 double c[N]; 10 bool check(double mid) 11 { 12 mid/=100; 13 for(int i=1;i<=n;i++) c[i] = a[i]-mid*b[i]; 14 sort(c+1,c+n+1); 15 double sum = 0; 16 for(int i=k+1;i<=n;i++) sum+=c[i]; 17 if(sum>=0) return 1; 18 return 0; 19 } 20 int main() 21 { 22 while(scanf("%d%d",&n,&k)!=EOF&&(n+k)) 23 { 24 for(int i=1;i<=n;i++) scanf("%d",&a[i]); 25 for(int j=1;j<=n;j++) scanf("%d",&b[j]); 26 double l = 0,r = 100; 27 while(r-l>=eps) 28 { 29 double mid = (l+r)/2; 30 if(check(mid)) l = mid; 31 else r = mid; 32 } 33 int ans = r+0.5; 34 printf("%d\n",ans); 35 } 36 return 0; 37 }