zoukankan      html  css  js  c++  java
  • 最大化平均值

    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 = {i1, i2, …, 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 ≤ kn ≤ 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 个,问怎么选平均总价值最大 ?

    思路 :

      先取前k个元素算出S0 =∑(vi/wi) 作为初始值 ,然后对每一个元素(n个)求s=vi-x*wi

      要体会二分搜索过程的关键点,二分的 mid 值就是你所要一直求得值。

    代码 :

    const int eps = 1e5+5;
    const double pi = acos(-1.0);
    const int inf = 0x3f3f3f3f;
    #define Max(a,b) a>b?a:b
    #define Min(a,b) a>b?b:a
    #define ll long long
    
    int n, k;
    struct node
    {
        int v, w, pt;
        double s;
        
        bool operator< (const node& ff)const{
            return s > ff.s;
        }
    }pre[eps];
    
    bool check(double x){
        for(int i = 1; i <= n; i++){
            pre[i].s = 1.0*pre[i].v - x*pre[i].w;
        }    
        sort(pre+1, pre+n+1);
        double sum = 0;
        for(int i = 1; i <= k; i++){
            sum += pre[i].s;
        }
        return sum >= 0;
    }
    
    int main() {
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", sttout);
        
        while(~scanf("%d%d", &n, &k)){
            for(int i = 1; i <= n; i++){
                pre[i].pt = i;
                scanf("%d%d", &pre[i].v, &pre[i].w);
            }
            double l = 0, r = 1.0*inf;
            
            for(int i = 1; i <= 200; i++){
                double mid = (l + r) / 2.0;
                if (check(mid)) l = mid;
                else r = mid;
            }
            
            for(int i = 1; i < k; i++){
                printf("%d ", pre[i].pt);
            }
            printf("%d
    ", pre[k].pt);
            //printf("%.2f
    ", ans);
        }
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    隐式图回溯法之八皇后问题解答
    试用O(n)来实现求出一个无序数组的中位数
    C++学习第一弹: const 指针 引用答疑
    一道面试题的解答_骑士获得金币问题
    根据已知词表选出好词(直通车)
    python3.5爬虫完成笔趣阁小说的爬取
    关于_水木社区[日经题]_10只狗鉴别1000瓶中哪一瓶药有毒_的解答思路
    数据库想法整合,多表联立交互
    [网络推广]直通车学习
    3行实现模糊匹配
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/7850938.html
Copyright © 2011-2022 走看看