#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxl 100010
#define inf 2000000001
#define eps 1e-7
using namespace std;
int n,k;
double ans;
double v[maxl],w[maxl];
struct node{double num;int pos;} a[maxl];
void prework()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%lf%lf",&v[i],&w[i]);
}
bool cmp(const node &x,const node &y)
{
return x.num>y.num;
}
bool jug(double x)
{
for(int i=1;i<=n;i++)
a[i].num=v[i]-w[i]*x,a[i].pos=i;
sort(a+1,a+1+n,cmp);
double sum=0;
for(int i=1;i<=k;i++)
sum+=a[i].num;
if(sum>eps)
return true;
else
return false;
}
void mainwork()
{
double l=eps,r=inf-eps,mid;
while(l+eps<r)
{
mid=(l+r)/2.0;
if(jug(mid))
l=mid;
else
r=mid;
}
ans=l;
}
void print()
{
jug(ans);
for(int i=1;i<=k;i++)
printf("%d ",a[i].pos);
}
int main()
{
prework();
mainwork();
print();
return 0;
}
Demy has n jewels. Each of her jewels has some value vi and weight wi.
Since her husband John got broke after recent financial crises, Demy has decided to sell some jewels. She has decided that she would keep k best jewels for herself. She decided to keep such jewels that their specific value is as large as possible. That is, denote the specific value of some set of jewels S = {i1, i2, …, ik} as
.
Demy would like to select such k jewels that their specific value is maximal possible. Help her to do so.
选出能使上式最大的k个珠子、
选出vi/wi最大的珠子无法满足vi之和/wi之和最大。于是我们二分这个最大值为x
然后 求一组(vi+vj....)/(wi+wj..)=x,移项得 (vi+vj...)-(wi+wj..)*x=0,而我们要求最大的x,所以只要(vi+vj...)-(wi+wj..)*x >= 0,这个x就是满足的。
本文介绍了一个算法,用于从一系列具有不同价值和重量的珠宝中选择最优的k个珠宝,使得它们的价值与重量比尽可能大。该算法通过二分查找确定最大价值权重比,并通过比较每个珠宝调整后的值来实现。

被折叠的 条评论
为什么被折叠?



