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;
}
}

POJ3111的更多相关文章

  1. POJ3111 K Best(另类背包+二分+变态精度)

    POJ3111 K Best,看讨论区说数据有点变态,精度要求较高,我就直接把循环写成了100次,6100ms过,(试了一下30,40都会wa,50是4000ms) 第一次在POJ上看到下面这种东西还 ...

  2. POJ-3111 K Best---二分求最大化平均值

    题目链接: https://cn.vjudge.net/problem/POJ-3111 题目大意: 卖宝救夫:Demy要卖珠宝,n件分别价值vi 重 wi,她希望保留k件使得 最大. 解题思路: # ...

  3. [POJ3111]K Best(分数规划, 二分)

    题目链接:http://poj.org/problem?id=3111 求选k对数,使得上述式子值最大.容易想到设左边为一个值,对式子变形以下,得到sigma(v-r*w))==0的时候就是最大的,& ...

  4. POJ3111 K Best

    Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...

  5. POJ3111 K Best 2017-05-11 18:12 31人阅读 评论(0) 收藏

    K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 10261   Accepted: 2644 Case Time ...

  6. POJ3111(最大化平均值)

    K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 8458   Accepted: 2163 Case Time ...

  7. 分数规划-poj3111

    题意:给定n个珠宝,每个珠宝有重量 w 和价值v ,要求你从中选出k个,使∑v/∑w 尽可能大,输出选出的珠宝的编号 数据范围: 1 ⩽ k ⩽ n ⩽ 10 , 1 ⩽ w , v ⩽ 10. 这道 ...

  8. poj3111 选取物品(二分+贪心)

    题目传送门 题意就是,n个物品,从中选取k个,要按照那个公式所取的值最大. 思路:最大化平均值的时候首先想到的就是二分, 我们设G(x) 为单位的重量不小于X, 我们的目标就是要找到满足条件的最大的X ...

  9. POJ3111 K Best —— 01分数规划 二分法

    题目链接:http://poj.org/problem?id=3111 K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissio ...

  10. poj3111 K Best 最大化平均值,二分

    题目:http://poj.org/problem?id=3111 题意:给你n,k,n个数的v.w值,选出k个数,使得v之和/w之和最大化. 思路:一看到题目,这不是赤果果的贪心吗?为什么放在二分专 ...

随机推荐

  1. PHP经典算法题

    1.百钱买百鸡 公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱,用100文钱买一百只鸡,其中公鸡,母鸡,小鸡都必须要有,问公鸡,母鸡,小鸡要买多少只刚好凑足100文钱. 分析:估计现在小学生都能手工推 ...

  2. 创建OData Service(基于ASP.NET 4.6.1, EF 6),Part I:Project initialize

    由于ASP.NET Core 1处于RC阶段,加上OData WebAPI 对ASP.NET Core 1的跟进不是很积极,基于ASP.NET Core 1的Alpha 1版本已经N月没有check ...

  3. C#语音特性 很实用

    1.隐式类型 Var 2.匿名类型 new{} 3.自动属性 [prop TAB]  public string Title { get; set; } 4.初始化器  var myObj1 = ne ...

  4. [ISE使用] 使用ISE的过程中,遇到过的一些“软件上的问题”

    1.planahead打不开了. PlanAhead替代文件rdiArgs.bat的下载链接如下: http://www.eevblog.com/forum/microcontrollers/guid ...

  5. bash:echo

    echo 'xxxx'自带换行 echo -n ‘xxxxxx’ 取消换行 echo -e "xxxxxxxxxxxx"允许转义字符(两种引号对转以字符效果相同,影响$变量) 转义 ...

  6. 如何在后台封装el-tree所需要的数据格式

    背景 最近遇到了一个分层级展示指标的需求,前端使用el-tree树形组件,要求按官方文档的格式提供数据. 数据格式: id: 1, label: '一级 1', children: id: 4, la ...

  7. ES6 Map 原理

    ES6的Map的键可以是任意的数据结构,并且不重复. 那么map的底层原理是啥呢? Map利用链表,hash的思想来实现. 首先,Map可以实现删除,而且删除的数据可以是中间的值.而链表的优势就是在中 ...

  8. boost.asio新框架的设计概念总结

    1.66版本,boost.asio库重新设计了框架,目前最新版为1.71.读了几天代码后,对框架中相关概念总结.因为是泛型编程的库,所以分析的概念层的设计. 可通过boost官方文档,strand的1 ...

  9. ASP.NET Core gRPC 使用 Consul 服务注册发现

    一. 前言 gRPC 在当前最常见的应用就是在微服务场景中,所以不可避免的会有服务注册与发现问题,我们使用gRPC实现的服务可以使用 Consul 或者 etcd 作为服务注册与发现中心,本文主要介绍 ...

  10. element 时间限制 结束时间大于开始时间 数组形式

    组件中 绑定focus时间 <el-form-item v-for="(item, index) in ruleForm.yunqiDateArr" :key="i ...