您的位置:首页 > 其它

CSU-ACM2017暑假集训比赛1 TD POJ3111

2017-07-27 21:10 423 查看
#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就是满足的。


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: