POJ 3111

来源:互联网 发布:有趣的书 知乎 编辑:程序博客网 时间:2024/06/07 00:51


K Best
Time Limit: 8000MS Memory Limit: 65536KTotal Submissions: 11555 Accepted: 2978Case 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

题意:

就是最大化平均值。


POINT:

二分答案。

二分搜索法模型:    条件C(x):可以挑选使得单位重量的物品价值不小于x->求满足条件的最大x->如何判断C(x)    价值和/重量和>=x    价值和-重量和*x>=0    和(价值-重量*x)>=0    可以对(价值-重量*x)的值进行贪心的选取,选取最大的k个 和>=0


#include <iostream>#include <algorithm>#include <stdio.h>#include <string.h>#include <math.h>using namespace std;const int maxn = 100100;typedef pair<double, int> pr;int n,k;int v[maxn];int w[maxn];int check(double x){    pr y[maxn];    for(int i=1;i<=n;i++){        y[i].first=v[i]-x*w[i];        y[i].second=i;    }    sort(y+1,y+1+n);    double sum=0;    for(int i=n;i>=n-k+1;i--){        sum+=y[i].first;    }    if(sum>0) return 1;    return 0;}void haha(double x){    pr y[maxn];    for(int i=1;i<=n;i++){        y[i].first=v[i]-x*w[i];        y[i].second=i;    }    sort(y+1,y+1+n);    for(int i=n;i>=n-k+1;i--){        if(i!=n) printf(" ");        printf("%d",y[i].second);    }    printf("\n");}void solve(){    double l=0,r=100000;    while(fabs(l-r)>1e-6){        double mid=(l+r)/2;        if(check(mid)){            l=mid;        }else{            r=mid;        }    }    haha(l);}int main(){    while(~scanf("%d %d",&n,&k)){        for(int i=1;i<=n;i++){            scanf("%d %d",&v[i],&w[i]);        }        solve();    }}


原创粉丝点击