POJ 3111:K Best(思维+二分)

来源:互联网 发布:仙侠世界cos捏脸数据 编辑:程序博客网 时间:2024/05/19 20:59

Gourmet and Banquet

Time limit8000 ms Case time limit 2000 ms Memory limit65536 kB


Problem 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 ≤ 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.

The gourmet can instantly switch between the dishes but only at integer moments in time. It is allowed to return to a dish after eating any other dishes. Also in every moment in time he can eat no more than one dish.

Sample Input

3 2
1 1
1 2
1 3

Sample Output

1 2


题意:

Demy有N件珠宝,价值和重量分别为 Vi 和 Wi,现在他想在其中选择K件,使得s(S)最大,问要选择那几件。

解题思路:

一开始以为是贪心,但事实上由ab>cd>ef 并不能得到 a+cb+d>c+ed+f ,所以这种方法是错误的。
对s(S)进行二分,设s(S)为x,用

viwix

作为判断x可行的条件,若满足,继续找大的,否则找小的。
根据这个条件可以得到
(viwix)0

因此,我们可以根据 yi=(viwix) 的值对珠宝排序,取前K个的和,若不小于0,则条件成立。


Code:

#include <iostream>#include <algorithm>#include <cstdio>using namespace std;const int maxn=100000+5;const double EPS=1e-8;int w[maxn];int v[maxn];int n,k;struct Node{    int no;    double y;}a[maxn];bool cmp(Node a,Node b){    return a.y>b.y;}bool f(double x){    for(int i=0;i<n;i++)    {        a[i].no=i+1;        a[i].y=v[i]-x*w[i];    }    sort(a,a+n,cmp);    double sum=0;    for(int i=0;i<k;i++)        sum+=a[i].y;    if(sum>=0)        return true;    return false;}int bs(){    double lo=0,hi=1e7,ans=0;    while(hi-lo>EPS)//注意精度    {        double mid=(hi+lo)/2.0;        if(f(mid))        {            ans=mid;            lo=mid;        }        else            hi=mid;    }    return ans;}int main(){    while(scanf("%d%d",&n,&k)!=EOF)    {        for(int i=0;i<n;i++)        {            a[i].no=i+1;            scanf("%d%d",&v[i],&w[i]);        }        bs();        for(int i=0;i<k;i++)        {            printf("%d",a[i].no);            if(i==k-1)                printf("\n");            else                printf(" ");        }    }    return 0;}