poj3111 K Best 【0-1分数规划】

K Best
Time Limit: 8000MS   Memory Limit: 65536K
Total Submissions: 7154   Accepted: 1875
Case 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 2
1 1
1 2
1 3

Sample Output

1 2

 

题意:给你N个珠宝已经每个珠宝的价值和重量,你只能取其中K个。问你取哪几个珠宝使sigma(v[i])/ sigma(w[i])最大。

 

 

 

方法一:构造函数 sigma(t[i]) = sigma(v[i]) - sigma(w[i]) * o。 然后 二分o值

 

这道题没有那么高精度,用整型就足够了。 我用的double跑了6s。。。

#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 o)
{
    for(int i = 0; i < N; i++)
        num[i].t = num[i].v - o * 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;
}

  

posted @ 2018-05-10 10:47  xianbeigg  阅读(98)  评论(0编辑  收藏  举报