描述


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. MyBatis3.1 学习教程

    昨天中午,突然有想要学习 MyBatis 的冲动,经过 1.5 天的研究和学习,再加上以前学过 I batis 的经验,很快就了解了这门技术. 写这篇教程,是想告诉那些想学却又怕学习不好的同学们, 其 ...

  2. Ext.Net学习笔记12:Ext.Net GridPanel Filter用法

    Ext.Net学习笔记12:Ext.Net GridPanel Filter用法 Ext.Net GridPanel的用法在上一篇中已经介绍过,这篇笔记讲介绍Filter的用法. Filter是用来过 ...

  3. js控制文本框输入数字和小数点等

    1.文本框只能输入数字代码(小数点也不能输入) <input onkeyup="this.value=this.value.replace(/\D/g,'')" onafte ...

  4. Java 的自动装箱拆箱

    Java 是面向对象的语言,其基本数据类型也就有了相对应的类,称为包装类.以下是基本数据类型对应的包装类: 基本数据类型 包装类 byte(1字节) Byte short(2字节) Short int ...

  5. HDU 1619 Unidirectional TSP(单向TSP + 路径打印)

    Unidirectional TSP Problem Description Problems that require minimum paths through some domain appea ...

  6. Codevs 5208 求乘方取模

    5208 求乘方取模 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 未定级 题目描述 Description 给定非负整数A.B.M,求(A ^ B) mod M. 输入描述 Inpu ...

  7. 关于Navicat导入MySQL数据库遇到的一些问题

    今天本想将之前的一个数据库easy.sql用图形化工具Navicat导入的,开始是用“运行SQL文件”导入,结果是“queries successfully”啥的,去查是否导表成功,一看并没有. 结果 ...

  8. 接口Interface

    接口的定义 声明接口的方式与声明类的方式相似,但使用的关键字是interface,而不是class interface IMyInterface { // 接口成员 } 访问修饰符关键字public ...

  9. 2016年1月编程语言排行榜:Java荣获2015年度冠军

    Java因于2015年人气增幅最大(+ 5.94%),故获得2015年的TIOBE指数的编程语言奖,同时成为15年年度冠军, Visual Basic.NET(+ 1.51%)和Python(+ 1. ...

  10. phpstorm调整背景、字体颜色

    从这个网站(http://phpstorm-themes.com/)下载各类主题的xml文件, 然后将文件放到phpStorm的文件夹中,比如:C:\Users\USERNAME\.PhpStorm2 ...