Description

  1. Demy has n jewels. Each of her jewels has some value vi and weight wi.
  2.  
  3. 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

.

  1. Demy would like to select such k jewels that their specific value is maximal possible. Help her to do so.

Input

  1. 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 ( k n ).
  2.  
  3. The following n lines contain two integer numbers each vi and wi ( vi , wi , both the sum of all vi and the sum of all wi do not exceed ).

Output

  1. Output k numbers the numbers of jewels Demy must keep. If there are several solutions, output any one.

Sample Input

  1.  

Sample Output

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=;
  23. double sum2=;
  24. for(int i=;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)==){
  33. for(int i=;i<n;i++){
  34. scanf("%lf%lf",&node[i].v,&node[i].w);
  35. node[i].id=i;
  36. }
  37.  
  38. double low=;
  39. double high=inf;
  40. for(int i=;i<;i++){
  41. double mid=(low+high)/;
  42. if(solve(mid)){
  43. low=mid;
  44. }
  45. else{
  46. high=mid;
  47. }
  48. }
  49. for(int i=;i<k;i++){
  50. printf("%d ",node[i].id+);
  51. }
  52. printf("\n");
  53. }
  54. return ;
  55. }

poj 3111 K Best (二分搜索之最大化平均值之01分数规划)的更多相关文章

  1. poj 2976 Dropping tests (二分搜索之最大化平均值之01分数规划)

    Description In a certain course, you take n tests. If you get ai out of bi questions correct on test ...

  2. NYOJ 914 Yougth的最大化【二分/最大化平均值模板/01分数规划】

    914-Yougth的最大化 内存限制:64MB 时间限制:1000ms 特判: No 通过数:3 提交数:4 难度:4 题目描述: Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从 ...

  3. POJ 2728 Desert King(最优比率生成树 01分数规划)

    http://poj.org/problem?id=2728 题意: 在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少? 思路: 最优比率生成树,也就是01分数规划,二分答案即可,题目很简 ...

  4. POJ.2728.Desert King(最优比率生成树 Prim 01分数规划 二分/Dinkelbach迭代)

    题目链接 \(Description\) 将n个村庄连成一棵树,村之间的距离为两村的欧几里得距离,村之间的花费为海拔z的差,求花费和与长度和的最小比值 \(Solution\) 二分,假设mid为可行 ...

  5. POJ 2728 Desert King(最优比率生成树, 01分数规划)

    题意: 给定n个村子的坐标(x,y)和高度z, 求出修n-1条路连通所有村子, 并且让 修路花费/修路长度 最少的值 两个村子修一条路, 修路花费 = abs(高度差), 修路长度 = 欧氏距离 分析 ...

  6. poj 3111 K Best 最大化平均值 二分思想

    poj 3111 K Best 最大化平均值 二分思想 题目链接: http://poj.org/problem?id=3111 思路: 挑战程序竞赛书上讲的很好,下面的解释也基本来源于此书 设定条件 ...

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

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

  8. POJ - 2976 Dropping tests(01分数规划---二分(最大化平均值))

    题意:有n组ai和bi,要求去掉k组,使下式值最大. 分析: 1.此题是典型的01分数规划. 01分数规划:给定两个数组,a[i]表示选取i的可以得到的价值,b[i]表示选取i的代价.x[i]=1代表 ...

  9. POJ - 3111 K Best 0-1分数规划 二分

    K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 12812   Accepted: 3290 Case Time ...

随机推荐

  1. 【git 问题小说说】 git add时候报错:LF will be replaced by CRLF

    本文来自:http://blog.csdn.net/loovejava/article/details/22114477 最近工作在window平台,不怎么使用命令行了所以导致很多命令都不熟悉啦 哈哈 ...

  2. unity3d 学习笔记(一)

    操作:按下shit 点击坐标轴中心 切换透视图 动画烘焙的概念:相当于把原来的控制器动画或者IK(骨骼)动画所有塌陷为逐帧动画,导出的时候必须选这一项 着色器:从技术的角度来看,着色器是渲染器的一个部 ...

  3. eclipse 中 maven3 创建web项目

    一.创建项目 1.Eclipse中用Maven创建项目 上图中Next 2.继续Next 3.选maven-archetype-webapp后,next 4.填写相应的信息,Packaged是默认创建 ...

  4. [RxJS] Creating Observable From Scratch

    Get a better understanding of the RxJS Observable by implementing one that's similar from the ground ...

  5. 从点亮一个LED开始,Cortex-A9裸机程序设计

    电路原理图: 如何点亮一个LED? 通过对原理图进行分析,我们能够发现给三极管的基极加上一个高点平时,三级管be结导通构成通路,此时二极管就点亮了.若要将LED熄灭只需取消高电平输出. 如何使三级管基 ...

  6. Java清洁:终结处理和垃圾回收

    一般情况:Java有垃圾回收机制负责回收无用对象占据的内存资源. 特殊情况:假定你的对象(并非使用new)获得一块特殊的内存区域,由于垃圾回收器只知道释放那些经由new分配的内存,所以它不知道如何释放 ...

  7. asp.net服务器向客户端弹出对话框,但不使页面边白板

    1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Web; 5: ...

  8. ajax请求webservice时抛出终止线程的异常

    请求webservice中以下接口,会抛出异常 {"Message":"正在中止线程.","StackTrace":" 在 Sys ...

  9. dispatch_async 与 dispatch_get_global_queue 的使用方法

    GCD (Grand Central Dispatch) 是Apple公司开发的一种技术,它旨在优化多核环境中的并发操作并取代传统多线程的编程模式. 在Mac OS X 10.6和IOS 4.0之后开 ...

  10. 为什么要用Math.sqrt(i)方法

    java 练习题 判断 101-200 之间有多少个素数,并输出所有素数 public class Prime { public static int count = 0; public static ...