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 = {i1i2, …, 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

题解:

AC代码为:

 个题很明显就是要最大化平均值,然而采用贪心的方法每次取单位价值最大的钻石,是显然不行的。所以应该采用二分搜索的方法,那么二分什么值最后才能得到答案呢?不妨这样想,S(x)表示最终的平均价值,那么就相当于找到一组(v,w)组合使得Σv/Σw≥S(x),移项得到不等式Σ(v-S(x)*w)≥0,这样一来就很容易发现直接二分S(x)即可,每次得到一个S(x)计算v-S(x)*w,之后排序,取前k个计算是否大于等于0,知道二分到一个S(x)使得不等式的值为0就是答案。
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <set> using namespace std; const int maxn = + ;
const double inf = + ;
int n,k;
struct jew
{
int id;
int v,w;
double key;
void cal(double x)
{
key = v - x * w;
} bool operator < (const jew& a) const
{
return key > a.key;
}
}j[maxn]; bool c(double x)
{
for (int i = ; i <= n; i++)
{
j[i].cal(x);
} sort(j+,j+n+); double tmp = ;
for (int i = ; i <= k; i++)
tmp += j[i].key;
return tmp >= ;
} int main()
{
while (cin>>n>>k)
{
for (int i = ; i <= n; i++)
{
scanf("%d%d",&j[i].v,&j[i].w);
j[i].id = i;
}
double l = , r = inf;
for (int i = ; i < ; i++)
{
double mid = (l + r) / ;
if (c(mid))
l = mid;
else
r = mid;
}
for (int i = ; i <= k; i++)
{
if (i - )
printf(" %d",j[i].id);
else
printf("%d",j[i].id);
} cout<<endl;
}
}

最新文章

  1. 微信小程序demo汇总
  2. DP专题训练之HDU 1087 Super Jumping!
  3. 获取layer.open弹出层的返回值
  4. Problem A CodeForces 556A
  5. shell local
  6. 在 eclipse 中设置每行的字数
  7. 使用sql生成UUID
  8. [Angular 2] Directive intro and exportAs
  9. PLSQL Package dubug方法
  10. js的数组操作
  11. NM_CUSTOMDRAW 消息
  12. 用C#代码实现类似QQ窗体的“上、左、右”停靠功能
  13. JavaScript 再谈闭包
  14. LoadRunner(四)——深度了解LR相关功能
  15. coreData的ManagedObject后,报错
  16. eclipse代码自动补全。
  17. iOS: 数据持久化方案
  18. 物联网架构成长之路(4)-EMQ插件创建
  19. 通过图片获取gps地理位置
  20. 10 个理由让你继续干 IT

热门文章

  1. Dart编程语言从基础到进阶1
  2. Apache httpd 2.4.27开启GZIP压缩功能
  3. Windows键盘无法调起
  4. [WPF] Caliburn Micro学习一 Installation
  5. Oracle instant client免安装Oracle客户端配置
  6. Batch批处理获取当前时间
  7. 微信小程序(mpvue) wx.openSetting 无法调起设置页面
  8. fastjason常用方法
  9. windows下自制动画层引擎 - 放两个demo
  10. pdf 在线预览之 vue-pdf插件