K Best POJ - 3111 二分 最大化平均值

Description

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 ≤ k ≤ n ≤ 100 000).

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

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

Sample Output

1 2

Hint

题意

n件物品 每件物品价值v重量w
问选择k件有最大平均值

题解:

二分枚举答案
判断
每个物品v减去这个值乘上w 再对前k个求和看是否大于等于0 说明这个值小于等于结果 左区间等于mid

代码

#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
const int N = 1e5+100;
const double eps = 1e-6;
int v[N];
int w[N];
int a[N];
double y[N];
int n,k;
/*按照a在y[i]中的大小排a[i]的序*/
bool cmp(int a,int b){
    return y[a]>y[b];
}
bool check(double x){
    for (int i = 1;i <= n; ++i){
        a[i] = i;
        y[i] = 1.0*v[i]-x*w[i];
    }
    sort(a+1,a+n+1,cmp);
    double sum = 0;
    for (int i = 1; i <= k; ++i){
        sum += y[a[i]];
    }
    if (sum>=0) return true;
    return false;
} 
int main(){
     scanf("%d%d",&n,&k);
     double r = 0;
     for (int i = 1; i <= n; ++i){
        scanf("%d%d",&v[i],&w[i]);
        r = max(r,v[i]*1.0/w[i]);
     }
     r+=10;
     double l = 0; 
     while (r-l>=eps){
        double mid = (l+r)/2;
        if (check(mid)) l = mid;
        else r = mid;
     }
     for (int i = 1;i <= k; ++i){
        printf("%d%c",a[i],i==k?'\n':' ');
     }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值