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 ...
随机推荐
- python 时间及日期函数
本人最近新学python ,用到关于时间和日期的函数,经过一番研究,从网上查找资料,经过测试,总结了一下相关的方法. import timeimport datetime '''时间转化为时间戳: 2 ...
- C# List<> 删除
List<string> l = new List<string>() { "A1", "A2", "A3", &q ...
- 用CURL来实现file_get_contents函数:curl_file_get_contents
<?php $url='http://www.bamuyu.com/news/zixun/list_7_2.html'; $content=curl_file_get_contents($url ...
- JavaSE replaceAll 方法
private String srcStr = "index\\.php\\?action=";//要替换的原字符串 private String destStr = " ...
- 关于checkbox的checked属性和change事件
jquery中的attr和prop有什么区别? To retrieve and change DOM properties such as the checked, selected, or disa ...
- 正确使用c语言中的头文件
我们在使用c编程的时候经常会遇到头文件,前段时间我自己做了个小项目的时候,也遇到了关于头文件的问题. 预处理器发现#include 指令后,就会寻找后跟的文件名并把这个文件包含的内容包含到当前文件中. ...
- oracle database resident connection pooling(驻留连接池)
oracle在11g中引入了database resident connection pooling(DRCP).在此之前,我们可以使用dedicated 或者share 方式来链接数据库,dedic ...
- VC C运行时库(CRTL)的几个版本及选用
分类: Windows 2008-12-23 10:01 987人阅读 评论(0) 收藏 举报ciostreammfclibrary多线程import最近做项目碰到了一个关于在动态库中使用MFC以及在 ...
- WCF - Versus Web Service
There are some major differences that exist between WCF and a Web service which are listed below. 这里 ...
- UltraEdit (Ctrl + F) 查找、(Ctrl + R)替换功能失效
环境: Windows 7 Service Pack 1 X64 工具: UltraEdit Version 21 症状: UltraEdit (Ctrl + F) 查找.(Ctrl + R)替换功能 ...