zoukankan      html  css  js  c++  java
  • poj 3111 K Best (二分搜索之最大化平均值之01分数规划)

    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 = {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 ≤ 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

    Source

    Northeastern Europe 2005, Northern Subregion
     
    这题和上一题基本上一样。只是要注意数的大小,数太大的话会超时
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<math.h>
     6 #include<stdlib.h>
     7 using namespace std;
     8 #define N 100006
     9 #define inf 1<<26
    10 int n,k;
    11 double x;
    12 struct Node{
    13     double v,w;
    14     int id;
    15     bool friend operator < (Node a,Node b){
    16         return a.v-x*a.w>b.v-x*b.w;
    17     }
    18 }node[N];
    19 bool solve(double mid){
    20     x=mid;
    21     sort(node,node+n);
    22     double sum1=0;
    23     double sum2=0;
    24     for(int i=0;i<k;i++){
    25         sum1+=node[i].v;
    26         sum2+=node[i].w;
    27     }
    28     return sum1/sum2>=mid;
    29 }
    30 int main()
    31 {
    32     while(scanf("%d%d",&n,&k)==2){
    33         for(int i=0;i<n;i++){
    34             scanf("%lf%lf",&node[i].v,&node[i].w);
    35             node[i].id=i;
    36         }
    37 
    38         double low=0;
    39         double high=inf;
    40         for(int i=0;i<100;i++){
    41             double mid=(low+high)/2;
    42             if(solve(mid)){
    43                 low=mid;
    44             }
    45             else{
    46                 high=mid;
    47             }
    48         }
    49         for(int i=0;i<k;i++){
    50             printf("%d ",node[i].id+1);
    51         }
    52         printf("
    ");
    53     }
    54     return 0;
    55 }
    View Code
  • 相关阅读:
    一分钟应对勒索病毒WannaCry
    你不知道网络安全有多严峻
    MongoDB 文章目录
    SQL Server 文章目录
    MySQL 文章目录
    领域驱动(DD)目录
    Oracle基本教程
    系统架构研究目录
    设计原则目录
    开源项目学习历程
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4782079.html
Copyright © 2011-2022 走看看