- 题目地址:
- 题目大意:给你n个物品的a值和b值,要你从中丢掉k个(或者说选择n-k个)物品,使得剩下的物品的最大
- 题目思路(以下思路转自):
我们能够看看例如以下推导
题目就变成了二分检索r
-
代码:
#include
#include #include #include #include #define MAXN 1010#define EPS (1e-6)#define INF 0x3f3f3f3fusing namespace std;double w[MAXN],v[MAXN];int n,k;double value[MAXN];bool check(double x) //推断平均值x是否可取{ double sum=0; for(int i=1;i<=n;i++) value[i]=v[i]-x*w[i]; sort(value+1,value+n+1); //贪心排序,从大到小选取k个value[i],且这k个value的和必须不小于0 for(int i=n;i>=n-k+1;i--) sum+=value[i]; return sum>=0;}int main(){ while(scanf("%d%d",&n,&k)!=EOF&&!(n==0&&k==0)) { k=n-k; for(int i=1;i<=n;i++) scanf("%lf",&v[i]); for(int i=1;i<=n;i++) scanf("%lf",&w[i]); double lowerBound=0,upperBound=INF; //二分这个平均值 while(fabs(upperBound-lowerBound)>EPS) { double mid=(lowerBound+upperBound)/2; if(check(mid)) lowerBound=mid; else upperBound=mid; } printf("%.f\n",100*upperBound); } return 0;}