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之和最大化. 思路:一看到题目,这不是赤果果的贪心吗?为什么放在二分专 ...
随机推荐
- pxe批量部署脚本
#!/bin/bash #检查环境 setenforce 0 sed -i 's/=enforce/=disabled/g' /etc/selinux/config systemctl restart ...
- 花一天时间试玩vsphere6.7(EXSI)服务器版的vmware
花一天时间试玩vsphere6.7(EXSI)服务器版的vmware 要注册账号(2019年11月14注册): 登陆网址:https://my.vmware.com/cn/group/vmware/h ...
- 使用CBrother的CLIB库调用windows的API
使用CBrother的CLIB库调用windows的API 2.1.0版本CBrother加入了CLib库,最新需要写一个工具,根据路径查杀一个Windows进程,研究了一下,CLib库的用法,感觉还 ...
- BST的实现(二叉搜索树)
void Inorder(struct Tree *T); //中序 void Preorder(struct Tree *T); //前序 void Postorder(struct Tree *T ...
- linux网络测试命令
一.ping 它通过向目标主机发送一个个数据包以及接受数据包的回应来判断主机和目标主机之间网络连接情况.ping的两个功能:判断网络是否可达.网络性能统计. ping使用的是网络层的ICMP协议. p ...
- 树的点分治 (poj 1741, 1655(树形dp))
poj 1655:http://poj.org/problem?id=1655 题意: 给无根树, 找出以一节点为根, 使节点最多的树,节点最少. 题解:一道树形dp,先dfs 标记 所有节点的子 ...
- Extjs导入Excel文件之后grid自动刷新显示刚插入的数据
var winUpload = new Ext.Window({ title: '导入excel文件', width: 400, height:200, listeners: { close: fun ...
- linux内核的preempt抢占调度,preempt_count抢占保护“锁”
抢断调度,是调度机制对实时系统需要的支持,是一种快速响应的重调度机制.既然与重调度有关,那么就先回顾一下调度和重调度. 调度分两种情况,1. 一种是自愿调度,由代码主动调用schedule来让度cpu ...
- 利用Python学习线性代数 -- 1.1 线性方程组
利用Python学习线性代数 -- 1.1 线性方程组 本节实现的主要功能函数,在源码文件linear_system中,后续章节将作为基本功能调用. 线性方程 线性方程组由一个或多个线性方程组成,如 ...
- windows下大数据开发环境搭建(1)——Hadoop环境搭建
所需环境 jdk 8 Hadoop下载 http://hadoop.apache.org/releases.html 配置环境变量 HADOOP_HOME: C:\hadoop-2.7.7 Path: ...