K Best
Time Limit: 8000MS   Memory Limit: 65536K
Total Submissions: 10507   Accepted: 2709
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 = {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

Source

Northeastern Europe 2005, Northern Subregion
题意:
有n个宝贝,每个宝贝有价值v和质量w要求取k个宝贝使得sum(v)/sum(w)最大,输出这k个宝贝的编号
代码:
//二分题,设最终结果是s,sum(分子)/sum(分母)=s,组成s的必然有a/b>=s和a/b<=s =>
//a-s*b>=0和a-s*b<=0,因此二分s值然后按照a-s*b从大到小排序依次取前k个看结果是否
//大于等于s
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
const double esp=0.000001;
int n,k;
double x;
struct Lu{
int id;
double v,w;
bool operator < (const Lu &p)const{
return v-x*w>p.v-x*p.w;
}
}L[maxn];
bool solve(double m){
x=m;
sort(L,L+n);
double tmp1=,tmp2=;
for(int i=;i<k;i++){
tmp1+=L[i].v;
tmp2+=L[i].w;
}
return tmp1-m*tmp2>=0.0;
}
int main()
{
while(scanf("%d%d",&n,&k)==){
for(int i=;i<n;i++){
scanf("%lf%lf",&L[i].v,&L[i].w);
L[i].id=i;
}
double l=0.0,r=10000000.0;
while(r-l>esp){
double m=(l+r)/2.0;
if(solve(m)){
l=m;
}else r=m;
}
for(int i=;i<k;i++)
printf("%d%c",L[i].id+,i==k-?'\n':' ');
}
return ;
}

POJ 3111 二分的更多相关文章

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

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

  2. POJ 3111 K Best(01分数规划)

    K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 9876   Accepted: 2535 Case Time ...

  3. POJ - 2018 二分+单调子段和

    依然是学习分析方法的一道题 求一个长度为n的序列中的一个平均值最大且长度不小于L的子段,输出最大平均值 最值问题可二分,从而转变为判定性问题:是否存在长度大于等于L且平均值大于等于mid的字段和 每个 ...

  4. POJ 3111 K Best(二分答案)

    [题目链接] http://poj.org/problem?id=3111 [题目大意] 选取k个物品,最大化sum(ai)/sum(bi) [题解] 如果答案是x,那么有sigma(a)>=s ...

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

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

  6. POJ 3111 K Best ( 二分 )

    题意 : 给出 N 个物品的价值和重量,然后要求选出 K 个物品使得选出来物品的单位重量价值最大,最后输出被选物品的编号. 分析 :  很容易去想先算出每个物品的单位价值然后升序排序取前 K 个,但是 ...

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

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

  8. POJ - 3111 K Best(二分)

    包含一些ai和bi的集用S来表示,x = max(sigma(ai)/sigma(bi),i 属于S) ,k 表示S的大小,k= |S|. x和k之间具有单调性.k0 < k1 → x0 ≥ x ...

  9. poj 3621 二分+spfa判负环

    http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...

随机推荐

  1. C++clock()延时循环

    函数clock(),返回程序开始执行后所用的系统时间,但是有两个复制问题. 1.clock()返回时间的单位不一定是秒 2.该函数的返回类型在某些系统上可能是Long,也可能是unsigned lon ...

  2. 机器学习(四)正则化与过拟合问题 Regularization / The Problem of Overfitting

    文章内容均来自斯坦福大学的Andrew Ng教授讲解的Machine Learning课程,本文是针对该课程的个人学习笔记,如有疏漏,请以原课程所讲述内容为准.感谢博主Rachel Zhang 的个人 ...

  3. BZOJ 4815 CQOI2017 小Q的表格 欧拉函数+分块

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4815 题意概述:要认真概述的话这个题就出来了... 分析: 首先分析题目,认真研究一下修 ...

  4. HDU 3262/POJ 3829 Seat taking up is tough(模拟+搜索)(2009 Asia Ningbo Regional)

    Description Students often have problems taking up seats. When two students want the same seat, a qu ...

  5. Sum of Consecutive Prime Numbers(素数打表+尺取)

    Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...

  6. 软件工程part5

    1.本周psp 2.本周饼状图 3.本周进度条

  7. 团队Beta阶段事后分析

    团队Beta阶段事后分析 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件要解决用户的休闲娱乐问题,为用户提供好玩的模拟经营类的游戏,游戏主题 ...

  8. Calculator Part Ⅰ (代码规范化修改)

    GitHub/object-oriented 本次参照的C++代码规范 有一些规范内容在文件中其实并未提及,比如:空格的使用,修改的时候真的是一头雾水--根据文件中的例子,发现了一些文字部分没有提到的 ...

  9. lintcode-33-N皇后问题

    33-N皇后问题 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击. 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案包含一个明确的n皇后放置布局,其中" ...

  10. 透过汇编另眼看世界之DLL导出函数调用

    前言:我一直对DLL技术充满好奇,一方面是因为我对DLL的导入/导出机制还不是特别的了解,另一面是因为我发现:DLL技术在Windows平台下占有重要的地位,几乎所有的Win32 API都是以导出函数 ...