K BEST(最大化平均值)

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

一.题解及分析

  1. 思路 : 对于这种最大化平均值类题目,应该利用v-w*s的和是否大于0来判断,然后二分s

  2. 注意:

    • 不能用贪心分别算v/w,再排序

    • 注意用结构体的时候排序时参数的形式,即sort的用法,但如果是结构体的话应该像以下这样排序,不然就报错(一定要加上struct)

      bool  cmp(struct  book a,struct book b);//一定要加上struct,不然就报错
      struct book
      {int v,w;double y;int index;
      } a[maxx];
      bool  cmp(struct book a,struct  book b)
      {return a.y<b.y;//注意排序的对象
      }
      sort(a+1,a+n+1,cmp);//sort函数的第三个参数表示
      ​`````
      

AC代码:

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxx=1000005;
int k,n;
double L,R;
int f(double x);
bool  cmp(struct  book a,struct book b);//一定要加上struct,不然就报错
struct book
{int v,w;double y;int index;
} a[maxx];
int main()
{scanf("%d%d",&n,&k);if(k==0) {printf("0");return 0;}    memset(a,0,sizeof(a));for(int i=1;i<=n;i++){scanf("%d%d",&a[i].v,&a[i].w);a[i].index=i;}int count=0;R=maxx;//一定不能写错了,如果这个写成R=0,L=maxx,就报错L=0;while(count<=50){count++;double mid=(L+R)/2 ;//注意是double型if(f(mid))  L=mid;else        R=mid;}printf("%d",a[n].index);for(int i=n-1;i>=n-k+1;i--)printf(" %d",a[i].index);return 0;
}
bool  cmp(struct book a,struct  book b)
{return a.y<b.y;
}
int  f(double x)
{double sum=0;for(int i=1;i<=n;i++){a[i].y=a[i].v-x*a[i].w;}sort(a+1,a+n+1,cmp);for(int i=n;i>=n-k+1;i--)sum+=a[i].y;if(sum>=0) return 1;return 0;
}

K BEST(最大化平均值)相关推荐

  1. POJ 2976 Dropping tests【二分 最大化平均值】

    题意:定义最大平均分为 (a1+a2+a3+---+an)/(b1+b2+---+bn),求任意去除k场考试的最大平均成绩 和挑战程序设计上面的最大化平均值的例子一样 判断是否存在x满足条件 (a1+ ...

  2. POJ 3111 K Best (最大化平均值,贪心 二分)难度⭐⭐⭐

    题目来源: [题意] 有n个物品的重量和价值分别是wi,vi,从中选取k个物品使得单位重量的价值最大. 输出格式: 输出一行物品的编号. #include<iostream> #inclu ...

  3. 转载二分 01 分数规划即最大化平均值的证明0/1分数规划、最优比率生成树、最优比率环

    首页 新随笔 联系 管理 订阅 随笔- 20  文章- 0  评论- 9 [Algorithm]01分数规划--Update:2012年7月27日 [关键字] 0/1分数规划.最优比率生成树.最优比率 ...

  4. 二分+01分数规划+最大化平均值 Dropping tests POJ - 2976

    题意: 给你若n个分数,分子a[i]a[i]a[i],分母b[i]b[i]b[i],使满足公式100⋅∑i=1nai∑i=1nbi100\cdot\tfrac{\sum_{i=1}^{n} a_{i} ...

  5. 最大化平均值 (二分搜索法)

    题目描述: 有n个物品的重量和价值分别为Wi和Vi.从中选出k个物品使得单位重量的价值最大. 例如: n=3 k=2 (w,v)={(2,2) , (5,3) , (2,1) } 输出应为0.75 分 ...

  6. 20190916CF训练

    A. Checkout Assistant 大意是你可以花c时间购买一个商品,然后得到任意的t的物品,最小化花费 一个类01背包,对每件物品枚举购买后时间结束的位置,用购买时的dp更新当前时间点 初始 ...

  7. Divide and conquer:K Best(POJ 3111)

     挑选最美的珠宝 题目大意:挑选k个珠宝使得∑a/∑b最大,输出组合数 最大化平均值的标准题型,二分法就好了,一定要注意范围(10e-7),如果是10e-8就会tle,10e-6就是wa 1 #inc ...

  8. POJ 3111 K Best 贪心 二分

    题目链接: http://poj.org/problem?id=3111 题目描述: 在N个物品中让你选出K个, 使得平均价值最大 解题思路: 这是我错的代码......一会儿回来改啊......一会 ...

  9. 独家 | R语言中K邻近算法的初学者指南:从菜鸟到大神(附代码&链接)

    作者:Leihua Ye, UC Santa Barbara 翻译:陈超 校对:冯羽 本文约2300字,建议阅读10分钟 本文介绍了一种针对初学者的K临近算法在R语言中的实现方法. 本文呈现了一种在R ...

最新文章

  1. 【c语言】求两数之和
  2. MySQL下载与MySQL安装图解(MySQL5.7与MySQL8.0)
  3. 箱线图的四分位怎么计算_Minitab图形 | 箱线图—3解释结果
  4. SpringMVC 上传文件and过滤器
  5. 根据年月日计算是星期几的函数,基姆拉尔森计算公式
  6. SqlServer2008备份与还原(完整图示版)
  7. 带你了解Node.js包管理工具:包与NPM
  8. 使用rsync完成内网数据备份
  9. Dockerfile best practices
  10. java.io.IOException: Filesystem closed
  11. yoga710怎么进入bios_联想笔记本怎么进入BIOS联想手提电脑进BIOS方法汇总
  12. 深度学习环境安装所需软件介绍cuda+cudnn+driver+anaconda+keras+tensorFlow+Pycharm+Jupyer(下载地址+配图)
  13. arcGis for js 3D marker
  14. 1021.Deepest Root
  15. 写一函数,将一个3*3的整型矩阵转置
  16. 从微信封杀拼多多链接浅谈我是如何解决微信屏蔽封杀外部以及广告链接的
  17. 【今日爆点】华为HDC开发者大会上正式发布深度欧拉V1.0
  18. 20175212童皓桢 《Java程序设计》第十周学习总结
  19. 计算机视觉(ComputerVision, CV)相关领域的网站链接
  20. Java消息队列三道面试题详解

热门文章

  1. 数据迁移工具 - Flyway
  2. invokedynamic指令
  3. curl post file PHP
  4. gulp + webpack + sass 学习
  5. Linux之使用网络
  6. 安装JDK出现问题 Error opening registry key'software\Javasoft\Java Runtime Environment'
  7. 出错提示:“Could not flush the DNS Resolver Cache: 执行期间,函数出了问题”的解决方法...
  8. 数据结构-栈(先进后出表)
  9. dataframe列互换 python_统计学原理之python数据分析基础
  10. ip在线代理联合早报_我所资深代理人马俪雯线上参加”中国医药设备工程协会年会”...