zoukankan      html  css  js  c++  java
  • poj 3111 K Best

    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

    最大化平均值:

    直接二分平均值x求出 最大的sigma(vi-x*wi) 然后判断是否大于等于0即可

     1 #include <algorithm>
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 using namespace std;
     8 const int N=100005;
     9 struct node{
    10     int w,v,id;
    11     double pst;
    12 }a[N];
    13 int n,k;
    14 int gi(){
    15     int str=0;char ch=getchar();
    16     while(ch>'9' || ch<'0')ch=getchar();
    17     while(ch>='0' && ch<='9')str=(str<<1)+(str<<3)+ch-48,ch=getchar();
    18     return str;
    19 }
    20 bool comp(const node &pp,const node &qq){return pp.pst>qq.pst;}
    21 bool check(double ff){
    22     for(int i=1;i<=n;i++){
    23         a[i].pst=a[i].v-a[i].w*ff;
    24     }
    25     sort(a+1,a+n+1,comp);
    26     double sum=0;
    27     for(int i=1;i<=k;i++)sum+=a[i].pst;
    28     if(sum>=0)return true;
    29     return false;
    30 }
    31 int main()
    32 {
    33     n=gi();k=gi();
    34     for(int i=1;i<=n;i++)a[i].v=gi(),a[i].w=gi(),a[i].id=i;
    35     double l=0,r=1e7,mid;double exp=1e-7;
    36     while(l<=r-exp){
    37         mid=(l+r)/2;
    38         if(check(mid))l=mid+exp;
    39         else r=mid-exp;
    40     }
    41     for(int i=1;i<=k;i++)printf("%d ",a[i].id);
    42     return 0;
    43 }
  • 相关阅读:
    给vs2012轻松换肤
    几种软件常用授权方式总结
    Discuz X2多人斗地主[消耗论坛积分]小体积版本,仅25MB!
    关于Socket 设置 IPAddress.Any 情况下,出现服务器积极拒绝的问题
    以前看过一个压缩过的.exe,运行会播放长达半小时的动画,却只有60KB,个人认为其中的原理
    VisualSvn Server安装和使用
    socket短时间内重连需注意的问题
    PostgreSQL在何处处理 sql查询之十一
    PostgreSQL在何处处理 sql查询之十三
    PostgreSQL在何处处理 sql查询之十四
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7146511.html
Copyright © 2011-2022 走看看