二分 POJ3111 K Best 最大化平均值

9 篇文章 0 订阅
9 篇文章 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.

Input

The first line of the input file contains n — the number of jewels Demy got, and k — the number of jewels she would like to keep (1 ≤ kn ≤ 100 000).

The following n lines contain two integer numbers each — vi and wi (0 ≤ vi ≤ 106, 1 ≤ wi ≤ 106, both the sum of all vi and the sum of all wi do not exceed 107).

Output

Output k numbers — the numbers of jewels Demy must keep. If there are several solutions, output any one.

Sample Input

3 2
1 1
1 2
1 3

 

n 个珍珠选k个使其s价值最大,如果每个都算s排序取前k个的话是不符合题意的,因为这无法保证总s价值最大,一开始想了好久没想过来,看了几篇题解才懂了。通过x(s)=v/w可得,y=x*w-v=0时,可得到最大s价值,所以我们可以二分x,计算每个的y并排序,即可得到答案。

 

/*更新:

相当于求解满足条件   \frac{\sum v}{\sum w} \geqslant s  , s最大值 及 此时的组合/选择集合,即  \sum\left (w * s - v \right ) \geqslant 0 ,二分s,求得可以满足条件的最大s下的集合即可。

 

/*新代码:

#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn=100020;
const double inf=0x3f3f3f3f;
const double eps=1e-8;

int n,k;
struct jewel{
	int num,v,w;
	double y;
}j[maxn];

bool cmp(jewel a,jewel b)
{
	return a.y<b.y;	//y尽可能小,越小special value越大??!
}

double cal(double x)
{
	double Y=0;
	for(int i=0;i<n;++i){
		j[i].y=j[i].w*x-j[i].v;
	}
	sort(j,j+n,cmp);
	for(int i=0;i<k;++i){
		Y+=j[i].y;
	}
	return Y;
}

int main()
{
	while(~scanf("%d%d",&n,&k)){
		for(int i=0;i<n;++i){
			scanf("%d%d",&j[i].v,&j[i].w);
			j[i].num=i+1;	//注+1
		}
		double left=0,right=inf,mid;
		while(right-left>eps){
			mid=(left+right)/2;
			if(cal(mid)>0)
				right=mid;	//x值大了
			else
				left=mid;	//x值小了
		}
		for(int i=0;i<k-1;++i){
			printf("%d ",j[i].num);
		}
		printf("%d\n",j[k-1].num);
	}

	return 0;
}

 

/*旧代码

#include <cstdio>
#include <algorithm>
using namespace std;

const double inf=99999999.0;
struct jew{
    long v;
    long w;
    int num;
    double y;
}j[100000];
int k,n;
double s;
const double eps=1e-8;

double cal(double x);
bool cmp(jew j1,jew j2);

int main()
{
    int i;
    double left,right,mid;

    scanf("%d%d",&n,&k);

    for(i=0;i<n;i++)
    {
        scanf("%d%d",&j[i].v,&j[i].w);
        j[i].num=i+1;
    }
    left=0;
    right=inf;    //注:左右取值
    while(left+eps<right){
        mid=(left+right)/2;
        if(cal(mid)>0)
            left=mid;
        else
            right=mid;
    }
    for(i=0;i<k-1;i++)
        printf("%d ",j[i].num);
    printf("%d\n",j[i].num);
}

double cal(double x)    二分x计算y
{
    int i;

    for(i=0;i<n;i++)
        j[i].y=j[i].v-x*j[i].w;
    sort(j,j+n,cmp);
    s=0;//!
    for(i=0;i<k;i++)
        s+=j[i].y;
    return s;
}

bool cmp(jew j1,jew j2)    //结构体排序比较函数
{
    return j1.y>j2.y;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值