zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    EasyDSS前端界面在页面缩小时内置列表仍需手动刷新的优化
    【解决方案】家庭保姆犯罪案频出,EasyDSS视频监控平台如何确保家政安全?
    EasyDSS现场录视频流合成后出现视频内容部分丢失的问题排查及解决
    TSINGSEE青犀视频基于流媒体技术EasyDSS搭建酒店IPTV直播/点播平台
    EasyDSS视频直播列表页面横向滚动条和纵向滚动条不能同步的问题优化
    【解决方案】电力巡检进入智能化时代,无人机+EasyDSS开启智能巡检新模式
    EasyDSS新内核版本测试删除录像文件后存在残留问题调整优化
    IT常识
    Java面试题+算法案例
    数据库理论概述
  • 原文地址:https://www.cnblogs.com/csushl/p/9386614.html
Copyright © 2011-2022 走看看