01分数规划
#include <bits/stdc++.h> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define ForkD(i,k,n) for(int i=n;i>=k;i--) #define MAXN (200000) #define eps (1e-6) int T; int f[MAXN],g[MAXN]; double e[MAXN]; int main() { // freopen("model2.in","r",stdin); cin>>T; For(kcase,T) { int n,k; scanf("%d%d",&n,&k); For(i,n) scanf("%d",&f[i]); For(i,n) scanf("%d",&g[i]); For(i,n) f[i]*=g[i]; double l=0,r=100; while((r-l)>eps) { double m=(r+l)/2,ans=0; For(i,n) e[i]=(double)f[i]-m*(double)g[i]; sort(e+1,e+1+n); ForkD(i,k+1,n) ans+=e[i]; ForD(i,k) { if (e[i]>0) ans+=e[i]; else break; } if (ans>=0) l=m; else r=m; } printf("Case #%d:\n%.3lf\n",kcase,l); } return 0; }
Chang'an(Hack-01分数规划)
时间: 2024-10-29 19:10:04