POJ 3111 二分
| 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 = {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 ≤ 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
//二分题,设最终结果是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 二分的更多相关文章
- poj 3111 K Best 最大化平均值 二分思想
poj 3111 K Best 最大化平均值 二分思想 题目链接: http://poj.org/problem?id=3111 思路: 挑战程序竞赛书上讲的很好,下面的解释也基本来源于此书 设定条件 ...
- POJ 3111 K Best(01分数规划)
K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 9876 Accepted: 2535 Case Time ...
- POJ - 2018 二分+单调子段和
依然是学习分析方法的一道题 求一个长度为n的序列中的一个平均值最大且长度不小于L的子段,输出最大平均值 最值问题可二分,从而转变为判定性问题:是否存在长度大于等于L且平均值大于等于mid的字段和 每个 ...
- POJ 3111 K Best(二分答案)
[题目链接] http://poj.org/problem?id=3111 [题目大意] 选取k个物品,最大化sum(ai)/sum(bi) [题解] 如果答案是x,那么有sigma(a)>=s ...
- POJ - 3111 K Best 0-1分数规划 二分
K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 12812 Accepted: 3290 Case Time ...
- POJ 3111 K Best ( 二分 )
题意 : 给出 N 个物品的价值和重量,然后要求选出 K 个物品使得选出来物品的单位重量价值最大,最后输出被选物品的编号. 分析 : 很容易去想先算出每个物品的单位价值然后升序排序取前 K 个,但是 ...
- POJ 3111 K Best 最大化平均值 [二分]
1.题意:给一共N个物品,每个物品有重量W,价值V,要你选出K个出来,使得他们的平均单位重量的价值最高 2.分析:题意为最大化平均值问题,由于每个物品的重量不同所以无法直接按单位价值贪心,但是目标值有 ...
- 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 ...
- poj 3621 二分+spfa判负环
http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...
随机推荐
- Vue-cli 工具 / 通过 Vue-cli 工具重构 todoList
本博文归纳在 Vue 学习过程中, Vue-cli 工具的使用说明.除此之外还通过 Vue-cli 工具将之前 Vuejs 基本语法当中实现的 todoList 进行重构. 安装 npm instal ...
- linux服务器操作小技巧
python程序后台一直运行,并将打印信息输出到文件中 nohup -u test.py > out.txt & -u 表示无缓冲,直接将打印信息输出带文件中 &表示程序后台运行
- POJ 2208 Pyramids(求四面体体积)
Description Recently in Farland, a country in Asia, a famous scientist Mr. Log Archeo has discovered ...
- 第一次课堂作业---circle
链接:circle
- iOS开发改变字符串中指定字符颜色,大小等等
NSString *strJTGZ = [NSString stringWithFormat:@"交通管制%d处 ",[jtgz intValue]]; NSMutableAttr ...
- activemq控制面板里的NumberOfPendingMessages、MessagesEnqueued、MessagesDequeued含义
Number Of Consumers 消费者 这个是消费者端的消费者数量.Number Of Pending Messages 等待消费的消息 这个是当前未出队列的数量.可以理解为总接收数-总出队列 ...
- 一日一句 SQL [持续更新] MySQL + Oracle
1 . group by 和 having字句: group by是根据列值对数据进行分组, having子句用于对分组的数据进行过滤. [ having 针对的对象是分好的组] eg: employ ...
- 【python】Python 之 __new__() 方法与实例化
本文转自:http://www.cnblogs.com/ifantastic/p/3175735.html __new__() 是在新式类中新出现的方法,它作用在构造方法建造实例之前,可以这么理解,在 ...
- RT-thread内核之邮箱
一.邮箱控制块:在include/rtdef.h中 #ifdef RT_USING_MAILBOX /** * mailbox structure */ struct rt_mailbox { str ...
- Swift学习与复习
swift中文网 http://www.swiftv.cn http://swifter.tips/ http://objccn.io/ http://www.swiftmi.com/code4swi ...