• POJ3111


    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

    题解:

    AC代码为:

     个题很明显就是要最大化平均值,然而采用贪心的方法每次取单位价值最大的钻石,是显然不行的。所以应该采用二分搜索的方法,那么二分什么值最后才能得到答案呢?不妨这样想,S(x)表示最终的平均价值,那么就相当于找到一组(v,w)组合使得Σv/Σw≥S(x),移项得到不等式Σ(v-S(x)*w)≥0,这样一来就很容易发现直接二分S(x)即可,每次得到一个S(x)计算v-S(x)*w,之后排序,取前k个计算是否大于等于0,知道二分到一个S(x)使得不等式的值为0就是答案。
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <vector>
     5 #include <cstring>
     6 #include <string>
     7 #include <map>
     8 #include <queue>
     9 #include <set>
    10 
    11 
    12 using namespace std;
    13 
    14 
    15 const int maxn = 100000 + 5;
    16 const double inf = 1000000 + 5;
    17 int n,k;
    18 struct jew 
    19 {
    20     int id;
    21     int v,w;
    22     double key;
    23     void cal(double x) 
    24 { 
    25 key = v - x * w; 
    26 }
    27 
    28     bool operator < (const jew& a) const 
    29 {
    30         return key > a.key;
    31     }
    32 }j[maxn];
    33 
    34 
    35 bool c(double x) 
    36 {
    37     for (int i = 1; i <= n; i++) 
    38 {
    39         j[i].cal(x);
    40     }
    41     
    42     sort(j+1,j+n+1);
    43     
    44     double tmp = 0;
    45     for (int i = 1; i <= k; i++) 
    46 tmp += j[i].key;
    47     return tmp >= 0;
    48 }
    49 
    50 
    51 int main() 
    52 {
    53     while (cin>>n>>k) 
    54 {
    55         for (int i = 1; i <= n; i++) 
    56 {
    57             scanf("%d%d",&j[i].v,&j[i].w);
    58             j[i].id = i;
    59         }
    60         double l = 0, r = inf;
    61         for (int i = 0; i < 100; i++) 
    62 {
    63             double mid = (l + r) / 2;
    64             if (c(mid)) 
    65 l = mid;
    66             else 
    67 r = mid;
    68         }
    69         for (int i = 1; i <= k; i++) 
    70 {
    71             if (i - 1) 
    72 printf(" %d",j[i].id);
    73             else 
    74 printf("%d",j[i].id);
    75         }
    76         
    77         cout<<endl;
    78     }
    79 }
    View Code
  • 相关阅读:
    POJ3468 A Simple Problem with Integers(线段树成段增减,区间求和)
    HDU1698 Just a Hook(线段树成段替换、区间求和,延迟标记的应用)
    HDU2795 Billboard(线段树基础题单节点更新区间查询)
    HDU1754 I Hate It(线段树基础题单节点更新区间查询)
    HDU5410 CRB and His Birthday(完全背包)
    HDU1166 敌兵布阵(线段树基础题单节点更新区间查询)
    cf C. Bombs
    cf B. Resort
    cf B. Color the Fence
    cf B. Little Dima and Equation
  • 原文地址:https://www.cnblogs.com/csushl/p/9386614.html
Copyright © 2020-2023  润新知