描述


http://poj.org/problem?id=3111

n个珠宝,第i个有价值v[i]和重量w[i],要从总选k个,使得这k个的(价值之和)/(重量之和)即平均价值最大,输出选中的珠宝编号.

K Best
Time Limit: 8000MS   Memory Limit: 65536K
Total Submissions: 8189   Accepted: 2087
Case Time Limit: 2000MS   Special Judge

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

Source

Northeastern Europe 2005, Northern Subregion

分析


二分.

最大化平均值(同POJ 2976 Dropping Tests).

不等式变形做就可以了.

注意:

1.算c的值的时候不用担心算爆,int会先被提升为double再做运算,所以r取INF也没关系,不会爆(最多是INF*INF).

2.但是r取INF精度就不够了,所以还是乖乖取max(a[i]/b[i])把...

3.排序的时候直接排成从大到小的,方便.

4.这题二分次数少了进度不够,次数多了超时.

 #include<cstdio>
#include<algorithm>
using std :: sort;
using std :: max; const int maxn=,INF=0x7fffffff;
int n,k;
int ans[maxn];
struct point
{
int a,b,num;
double c;
}j[maxn];
double x,y; bool comp(point x,point y) { return x.c>y.c; } bool C(double x)
{
for(int i=;i<=n;i++) j[i].c=j[i].a-j[i].b*x;
sort(j+,j+n+,comp);
double sum=;
for(int i=;i<=k;i++) sum+=j[i].c;
return sum>=;
} void solve()
{
for(int i=;i<;i++)
{
double m=x+(y-x)/;
if(C(m))
{
x=m;
for(int i=;i<=k;i++) ans[i]=j[i].num;
}
else y=m;
}
for(int i=;i<k;i++) printf("%d ",ans[i]);
printf("%d",ans[k]);
} void init()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%d%d",&j[i].a,&j[i].b);
j[i].num=i;
y=max(y,(double)j[i].a/(double)j[i].b);
}
} int main()
{
freopen("k.in","r",stdin);
freopen("k.out","w",stdout);
init();
solve();
fclose(stdin);
fclose(stdout);
return ;
}

POJ_3111_K_Best_(二分,最大化平均值)的更多相关文章

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

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

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

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

  3. POJ:2976 Dropping tests(二分+最大化平均值)

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

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

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

  5. POJ 2976 3111(二分-最大化平均值)

    POJ 2976 题意 给n组数据ai,bi,定义累计平均值为: 现给出一个整数k,要求从这n个数中去掉k个数后,最大累计平均值能有多大?(四舍五入到整数) 思路 取n−k个数,使得累计平均值最大. ...

  6. poj 2976(二分搜索+最大化平均值)

    传送门:Problem 2976 参考资料: [1]:http://www.hankcs.com/program/cpp/poj-2976-dropping-tests-problem-solutio ...

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

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

  8. 二分算法的应用——最大化平均值 POJ 2976 Dropping tests

    最大化平均值 有n个物品的重量和价值分别wi 和 vi.从中选出 k 个物品使得 单位重量 的价值最大. 限制条件: <= k <= n <= ^ <= w_i <= v ...

  9. POJ 3111 K Best 最大化平均值 [二分]

    1.题意:给一共N个物品,每个物品有重量W,价值V,要你选出K个出来,使得他们的平均单位重量的价值最高 2.分析:题意为最大化平均值问题,由于每个物品的重量不同所以无法直接按单位价值贪心,但是目标值有 ...

随机推荐

  1. 使用ROW_NUMBER进行的快速分页

    DECLARE @pageSize INT ; DECLARE @pageIndex INT ; SET @pageSize = 5 SET @pageIndex =2 ; --第二页,每页显示5条数 ...

  2. Emgu CV的一个异常的解决方法

    今年组里有大项目落我头上了,并不能像去年一样回家还能搞搞Cocos2dX,一把老泪流了下来... 回到正题,由于组里需要做一个显示板的自动测试项目,涉及到Computer Vision.不得不说,这才 ...

  3. webapi之jsonp调用

    定义跨域handle public class CorsHandler : DelegatingHandler { const string Origin = "Origin"; ...

  4. HDU 2502 月之数(简单递推)

    月之数 Problem Description 当寒月还在读大一的时候,他在一本武林秘籍中(据后来考证,估计是计算机基础,狂汗-ing),发现了神奇的二进制数.如果一个正整数m表示成二进制,它的位数为 ...

  5. 远程mysql出现ERROR 1130 (HY000): Host '172.17.42.1' is not allowed to connect to this MySQL server

    ERROR 1130: Host ***.***.***.*** is not allowed to connect to this MySQL server 说明所连接的用户帐号没有远程连接的权限, ...

  6. ubuntu 在XP下硬盘安装

    以下选择在XP下用 grub4dos 安装 ubuntu 12.04版本 需要下载两个文件:一个是grub4dos,另一个是 ubutuntu 镜像文件 grub4dos下载地址:http://dow ...

  7. mysql 事务类型表的用法

    mysql关联表(references)的条件:1.两个表必须是 InnoDB表类型2.使用在外键关系的域必须为索引型(Index)3.使用外键关系的域必须与数据类型相似 以下是父表和子表的例子:创建 ...

  8. html中th 与thead tbody的 使用

    上午工作的时候,遇到一挺纠结的问题,在<th width...> width根本不起作用. 后来才明白<th>标签不能写在<tbody>里,不符合语法. 所以顺便总 ...

  9. [转]PHP Session原理分析及使用

    之前在一个叫魔法实验室的博客中看过一篇<php session原理彻底分析>的文章,作者从session的使用角度很好阐述了在代码运行过程中,每个环节的变化以及相关参数的设置及作用.本来想 ...

  10. WPF界面特殊字符处理

          界面XAML不支持< .>.&."等字符. 使用字符实体编码进行替代,以下是pro WPF 4.5的摘要表 Special Character        ...