POJ3111
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
题解:
AC代码为:
#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的更多相关文章
- 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
		
Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...
 - 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 Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 8458 Accepted: 2163 Case Time ...
 - 分数规划-poj3111
		
题意:给定n个珠宝,每个珠宝有重量 w 和价值v ,要求你从中选出k个,使∑v/∑w 尽可能大,输出选出的珠宝的编号 数据范围: 1 ⩽ k ⩽ n ⩽ 10 , 1 ⩽ w , v ⩽ 10. 这道 ...
 - poj3111 选取物品(二分+贪心)
		
题目传送门 题意就是,n个物品,从中选取k个,要按照那个公式所取的值最大. 思路:最大化平均值的时候首先想到的就是二分, 我们设G(x) 为单位的重量不小于X, 我们的目标就是要找到满足条件的最大的X ...
 - 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之和最大化. 思路:一看到题目,这不是赤果果的贪心吗?为什么放在二分专 ...
 
随机推荐
- PowerDesigner列名、注释内容互换
			
资料来源:PowerDesigner列名.注释内容互换 文中一共提供了2种操作的代码. (1)将Name中的字符COPY至Comment中 (2)将Comment中的字符COPY至Name中 使用方法 ...
 - Ios第三方FMDB使用说明
			
SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库.iOS SDK很早就支持了SQLite,在使用时,只需要加入 libsqlite3.dyli ...
 - ubuntu开机自启动服务
			
ubuntu下一个用来管理开机自启动服务的程序,今天在ss vps上安装时老是提示这个错误,百度后,下面的这个方法可行: vi /etc/apt/source.list 输入i,进入Insert模式 ...
 - 019.Kubernetes二进制部署插件dashboard
			
一 修改配置文件 1.1 下载解压 [root@k8smaster01 ~]# cd /opt/k8s/work/kubernetes/ [root@k8smaster01 kubernetes]# ...
 - 理解clientWidth,offsetWidth,clientLeft,offsetLeft,clientX,offsetX,pageX,screenX
			
1. clientWidth:表示元素的内部宽度,以像素计.该属性包括内边距,但不包括垂直滚动条(如果有).边框和外边距.(clientWidth = width + padding) 2. offs ...
 - 网页版的支付宝授权登录(vue+java)
			
api接口文档:https://docs.open.alipay.com/289/105656 后台管理系统原本是用账号密码登录的,不过需求要改成支付宝授权, 前端仅仅需要改登录页,以及添加一个授权返 ...
 - dubbo分布式Service不可以创建Error creating bean with name 'XXXXXX'
			
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'demoService' ...
 - linux 如何把一个装好的系统做成镜像(文件备份)
			
linux 如何把一个装好的系统做成镜像(文件备份)  我来答 浏览 11851 次来自电脑网络类芝麻团 2016-01-19 案例1(命令式操作) 像'ghost'那些备份系统,系统出了问题就恢复 ...
 - 分享一个撩妹、装13神技能,0基础用Python暴力破解WiFi密码
			
WiFi密码Python暴力破解 Python密码破解部分截图 获取视频资料,转发此文+点击喜欢,然后获取资料请加Python交流群:580478401,就可以获取视频教程+源码 环境准备: py ...
 - 使用lib-flexible.js适配移动端UI设计750px设计图
			
最近在和设计沟通关于设计图尺寸大小和前端实际页面尺寸大小不一致的情况,我们的UI设计是使用的iPone6的,(iphone6: 375px*667px 实际像素:750px*1334px)如果 ...