D - K Best

最大化平均值问题。

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 2
1 1
1 2
1 3
Sample Output
1 2


思路:看到这题,竟然第一反应是按比值排序,然后贪心,做到部份背包问题,后来发现并没有一个W值,很尴尬。

其实这就是很典型的最大化平均值问题,思想其实很简单,就是猜,然后验证这个值是否满足题意,如果满足题意呢,就说明该值满足,我可以去试试更大的值满不满足题意这时候就把L值变大,如果不满足题意呢,对应的就把R值变小,找到满足题意的值,这时候就要自己设置循环的次数,这里我就直接用了eps来做。但要选择好适当的初始R值。

我们设G(x) 为单位的重量不小于X, 我们的目标就是要找到满足条件的最大的X,也就是说我们的judge函数就要判断是否能够找到一个大小为K的集合S, 使得单位重量的价值大于等于X, 我们将当时变形转化为vi - x*wi >= 0  对于所有的i 属于集合S, 这时我们对于一个整体值vi-x*wi的值的 排序, 贪心的选取前K个。

#include <cstdio>  
#include <cstring>  
#include <cmath>  
#include <algorithm>  
#define MAXN 100000+10  
#define eps 1e-8  
using namespace std;  
struct Node  
{  
    double v, w, t;  
    int id;  
};  
Node num[MAXN];  
int N, K;  
bool cmp(Node a, Node b)  
{  
    return a.t > b.t;  
}  
bool judge(double mid)  
{  
    for(int i = 0; i < N; i++)  
        num[i].t = num[i].v - mid * num[i].w;  
    sort(num, num+N, cmp);  
    double sum = 0;  
    for(int i = 0; i < K; i++)//取前K个最大的  
        sum += num[i].t;  

    return sum >= 0;  

}  
int main()  
{  
    while(scanf("%d%d", &N, &K) != EOF)  
    {  
        double r = 0;  
        for(int i = 0; i < N; i++)  
        {  
            scanf("%lf%lf", &num[i].v, &num[i].w);  
            num[i].id = i + 1;  
            r = max(r, num[i].v / num[i].w);  
        }  
        double l = 0;  
        while(r - l >= eps)  
        {  
            double mid = (l + r) / 2;  
            if(judge(mid))  
                l = mid;  
            else  
                r = mid;  
        }  
        for(int i = 0; i < K; i++)  
        {  
            if(i > 0) printf(" ");  
            printf("%d", num[i].id);  
        }  
        printf("\n");  
    }  
    return 0;  
}  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值