牛客 wyh的物品 ###K ###K //K
题目链接:https://ac.nowcoder.com/acm/problem/15446
01分数规划问题
二分 check mid最大值 要使得 sum v / sum w >=mid 那么移项得 sum (v[i]-w[i]*mid)>=0
那么 把 v[i]-w[i]*mid 整体当一个数存起来 排序后 取前k大个 只要sum>=0 即合法
1 #include<bits/stdc++.h> 2 #define ll long long 3 #define pb push_back 4 using namespace std; 5 const int maxn=1e5+10; 6 const int mod=1e9+7; 7 double w[maxn],v[maxn]; 8 int n,k; 9 int check(double x) 10 { 11 vector<double>temp; 12 for(int i=1;i<=n;i++) 13 { 14 double cnt=v[i]-x*w[i]; 15 temp.pb(cnt); 16 } 17 sort(temp.begin(),temp.end(),greater<double>()); 18 double sum=0; 19 for(int i=0;i<k;i++) 20 { 21 sum+=temp[i]; 22 } 23 if(sum>=0) 24 return 1; 25 return 0; 26 } 27 28 29 30 int main() 31 { 32 ios::sync_with_stdio(0); 33 cin.tie(0); 34 int t; 35 cin>>t; 36 while(t--) 37 { 38 cin>>n>>k; 39 for(int i=1;i<=n;i++) 40 { 41 cin>>w[i]>>v[i]; 42 } 43 double l=0,r=1e9; 44 double ans=0; 45 for(int i=0;i<100;i++) 46 { 47 double mid=(l+r)/2.0; 48 if(check(mid)) 49 { 50 ans=mid; 51 l=mid; 52 } 53 else 54 r=mid; 55 } 56 cout<<fixed<<setprecision(2)<<ans<<'\n'; 57 } 58 59 60 61 }

浙公网安备 33010602011771号