POJ

来源:互联网 发布:你遇到尴尬的事 知乎 编辑:程序博客网 时间:2024/05/21 08:36
K Best
Time Limit: 8000MS Memory Limit: 65536KTotal Submissions: 11571 Accepted: 2983Case Time Limit: 2000MS Special Judge

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 = {i1i2, …, 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 ≤ 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 21 11 21 3

Sample Output

1 2

Source

Northeastern Europe 2005, Northern Subregion

题意:有n个珠宝的价值和重量分别是vi和wi,从中选出k个珠宝,使得 ∑v / ∑w 最大,求使得其最大的所选的k个珠宝


假设∑v / ∑w ≥ x 则问题便转化成 找到最大的使得不等式 ∑v / ∑w ≥ x 成立的 x

∑v / ∑w ≥ x  = > ∑v ≥ x * ∑w = ∑ (x*w)  = > ∑ (v - x * w) ≥  0

利用二分查找求得最大x


#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn = 100000+100;int n,k,v[maxn],w[maxn],s[maxn];struct node{    double s;int pos;}a[maxn];bool cmp(node a,node b) {return a.s>b.s;}int main(){    scanf("%d%d",&n,&k);    for(int i=0;i<n;i++) scanf("%d%d",&v[i],&w[i]);    double l = 0,r = 1e7,mid = (l+r)/2;    // > 1e-6 时会有精度误差    while(1e-6<r-l){        mid = (l+r)/2;        for(int i=0;i<n;i++){            a[i].s = v[i] - mid * w[i];            a[i].pos = i+1;        }        sort(a,a+n,cmp);        double sum = 0;        for(int i=0;i<k;i++) sum += a[i].s;        if(sum>=0) l = mid;        else r = mid;    }    for(int i=0;i<k;i++){        if(i) printf(" ");        printf("%d",a[i].pos);    }    printf("\n");    return 0;}








原创粉丝点击