POJ3111 K Best
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
最近刚学习了01规划,嗯,这差不多是裸题,然后,没有用binary_search ,学习了Dinkelbach ,其实感觉很迷吧, 会用就行了?
嗯,后面会多练习几道类似的题 。
#include <algorithm>
#include <iostream>
#include <cstdio>
const int N = + ;
using namespace std ;
int n , k , maxn ;
struct node
{
int v , w , id ; double val ;
bool operator < ( const node a ) const { return val > a.val ; }
} num[N] ; void Init( )
{
scanf( "%d%d" , &n , &k ) ;
for( int i = ; i <= n ; ++i )
{ scanf( "%d%d" , &num[i].v , &num[i].w ) ;
num[i].id = i ;
}
} double abse( double a )
{
return ( a < ) ? -a : a ;
} double search( double l )
{
for( int x = ; x <= n ; ++x ) num[x].val = (double)num[x].v - (double)num[x].w * l ;
sort( num + , num + + n ) ;
int tv = , tw = ;
for( int i = ; i <= k ; ++i )
tv += num[i].v , tw += num[i].w ;
double ret = (double)tv/(double)tw ;
return ret ;
} void Solve( )
{
double ans = , tmp ;
while( )
{
tmp = search( ans ) ;
if( abse( ans - tmp ) < 0.001 ) break ;
ans = tmp ;
}
for( int x = ; x <= k ; ++x )
printf( "%d " , num[x].id ) ;
puts( "" ) ; } int main( )
{
Init( ) ;
Solve( ) ;
return ;
}
POJ3111 K Best的更多相关文章
- POJ3111 K Best(另类背包+二分+变态精度)
POJ3111 K Best,看讨论区说数据有点变态,精度要求较高,我就直接把循环写成了100次,6100ms过,(试了一下30,40都会wa,50是4000ms) 第一次在POJ上看到下面这种东西还 ...
- POJ-3111 K Best---二分求最大化平均值
题目链接: https://cn.vjudge.net/problem/POJ-3111 题目大意: 卖宝救夫:Demy要卖珠宝,n件分别价值vi 重 wi,她希望保留k件使得 最大. 解题思路: # ...
- [POJ3111]K Best(分数规划, 二分)
题目链接:http://poj.org/problem?id=3111 求选k对数,使得上述式子值最大.容易想到设左边为一个值,对式子变形以下,得到sigma(v-r*w))==0的时候就是最大的,& ...
- 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 ...
- POJ3111 K Best —— 01分数规划 二分法
题目链接:http://poj.org/problem?id=3111 K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissio ...
- poj3111 K Best 最大化平均值,二分
题目:http://poj.org/problem?id=3111 题意:给你n,k,n个数的v.w值,选出k个数,使得v之和/w之和最大化. 思路:一看到题目,这不是赤果果的贪心吗?为什么放在二分专 ...
- django模型操作
Django-Model操作数据库(增删改查.连表结构) 一.数据库操作 1.创建model表
- 基于改进人工蜂群算法的K均值聚类算法(附MATLAB版源代码)
其实一直以来也没有准备在园子里发这样的文章,相对来说,算法改进放在园子里还是会稍稍显得格格不入.但是最近邮箱收到的几封邮件让我觉得有必要通过我的博客把过去做过的东西分享出去更给更多需要的人.从论文刊登 ...
- 【开源】专业K线绘制[K线主副图、趋势图、成交量、滚动、放大缩小、MACD、KDJ等)
这是一个iOS项目雅黑深邃的K线的绘制. 实现功能包括K线主副图.趋势图.成交量.滚动.放大缩小.MACD.KDJ,长按显示辅助线等功能 预览图 最后的最后,这是项目的开源地址:https://git ...
随机推荐
- VS2012格式化插件配置备份
VS2012联机插件AStyle --style=allman --indent=spaces=4 --align-pointer=type --align-reference=type --max- ...
- AT&T 和 Intel 汇编语法的主要区别
转自AT&T 和 Intel 汇编语法的主要区别 作为一个爱折腾的大好青年,补番之余还要补一些 Linux 下的基础,比如 GDB 的正确使用方法.但无论是看 gdb 还是 gcc -S 里的 ...
- LINUX Shell 下求两个文件交集和差集的办法
http://blog.csdn.net/autofei/article/details/6579320 假设两个文件FILE1和FILE2用集合A和B表示,FILE1内容如下: a b c e d ...
- [codility]PrefixMaxProduct
Codility Certificate题目.求product最大值,product为长度*出现次数,例子"abababa"如下: "a", whose pro ...
- [jobdu]矩形覆盖
推导一下,就是斐波那契数列那样的.但是要注意的是,int存不下,算一下需要long long才行,因为是指数级上升的. #include <cstdio> #define LEN 75 # ...
- 让QT编译快一点(增加基础头文件)
姚冬,中老年程序员 进藤光.杨个毛.欧阳修 等人赞同 我是来反对楼上某些答案的.我曾经用MFC写了金山词霸(大约20多万行),又用Qt写了YY语音(大约100多万行),算是对两种框架都比较有经验.纠正 ...
- ruby 编写迭代器
class My def initialize(name,age) @name=name @age=age end def sayName puts @name end def sayAge puts ...
- openwrt开发
之前写过一篇日志,是关于如何搭建自己的OpenWRT开发环境.经过最近一段时间的开发学习和实践,对OpenWRT环境的开发有了一定的了解.在这里将我的开发心得做个整理. 1.搭建开发环境 首先,我们需 ...
- bzoj1054
弱弱的搜索题, 我的做法是将矩阵看做二进制然后用位运算来做的,感觉比较舒服 ..] ,,,); dy:..] ,,-,); type node=record po,next: ...
- Bat 中特殊符号
批处理.Bat 中特殊符号的实际作用,Windows 批处理中特殊符号的作用: @\\隐藏命令的回显. ~\\在for中表示使用增强的变量扩展:在set中表示使用扩展环境变量指定位置的字符串:在set ...