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
  • 相关阅读:
    字符编码 乱码问题
    Django ORM那些相关操作
    pymysql模块使用---Python连接MySQL数据库
    数据库MySQL 之 索引原理与慢查询优化
    数据库MySQL之 视图、触发器、存储过程、函数、事务、数据库锁、数据库备份、事件
    数据库 MySQL 之 数据操作
    数据库 MySQL 之 表操作、存储引擎
    [BZOJ 4212]神牛的养成计划(Trie+可持久化Trie)
    [LuoguP4094] [HEOI2016] [TJOI2016]字符串(二分答案+后缀数组+ST表+主席树)
    [BZOJ 2865]字符串识别(后缀数组+线段树)
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4782079.html
Copyright © 2011-2022 走看看