zoukankan      html  css  js  c++  java
  • 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("
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    table中tr间距的设定table合并单元格 colspan(跨列)和rowspan(跨行)
    使用jquery触发a标签跳转
    真正的让iframe自适应高度 兼容多种浏览器随着窗口大小改变
    html5 data属性的使用
    jQuery取得select选择的文本与值
    jqueryui教程
    密码复杂度
    zabbix配置微信报警
    tomcat配置域名访问
    阿里云ecs禁止ping,禁止telnet
  • 原文地址:https://www.cnblogs.com/passion-sky/p/9018461.html
Copyright © 2011-2022 走看看