题目链接:
https://cn.vjudge.net/problem/POJ-2976
题目大意:
给定n个二元组(a,b),扔掉k个二元组,使得剩下的a元素之和与b元素之和的比率最大
解题思路:
扔掉k个球最大化平均值就是求取n-k个的最大化平均值
直接用最大化平均值模板
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstring> 6 using namespace std; 7 typedef long long ll; 8 const int maxn = 1e3 + 10; 9 int n, k; 10 int a[maxn], b[maxn]; 11 double c[maxn]; 12 bool judge(double x) 13 { 14 for(int i = 0; i < n; i++) 15 c[i] = a[i] - x * b[i]; 16 sort(c, c + n); 17 double sum = 0; 18 for(int i = 0, j = n - 1; i < k; i++, j--)//前k个大的数 19 sum += c[j]; 20 return sum >= 0; 21 } 22 int main() 23 { 24 while(cin >> n >> k && n) 25 { 26 for(int i = 0; i < n; i++)scanf("%d", &a[i]); 27 for(int i = 0; i < n; i++)scanf("%d", &b[i]); 28 k = n - k; 29 double l = 0, r = 1; 30 for(int i = 0; i < 50; i++) 31 { 32 double mid = (l + r) / 2; 33 if(judge(mid))l = mid; 34 else r = mid; 35 } 36 printf("%.f\n", 100 * l); 37 } 38 return 0; 39 }
原文地址:https://www.cnblogs.com/fzl194/p/9021794.html
时间: 2024-10-05 05:30:19