K Best POJ

来源:互联网 发布:公路基础数据库系统 编辑:程序博客网 时间:2024/06/07 10:14

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;}
原创粉丝点击