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

#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 = {i1i2, …, 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就是满足的。

 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值