POJ3111(最大化平均值)
| Time Limit: 8000MS | Memory Limit: 65536K | |
| Total Submissions: 8458 | Accepted: 2163 | |
| 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
注意要用G++提交
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;
const int MAXN=;
const double EPS=1.0e-6;
struct Node{
int id,v,w;
}a[MAXN];
struct Dou{
int id;
double y;
}b[MAXN];
bool comp(const Dou &no1,const Dou &no2)
{
return no1.y > no2.y;
}
int n,k;
bool test(double x)
{
for(int i=;i<n;i++)
{
b[i].id=a[i].id;
b[i].y=a[i].v-x*a[i].w;
}
sort(b,b+n,comp);
double sum=;
for(int i=;i<k;i++)
{
sum+=b[i].y;
}
return sum>=0.0;
}
int main()
{
while(cin>>n>>k)
{
for(int i=;i<n;i++)
{
a[i].id=i+;
cin>>a[i].v>>a[i].w;
}
double l=0.0;
double r=0x3f3f3f3f;
while(r-l>EPS)
{
double mid=(l+r)/;
if(test(mid)) l=mid;
else r=mid;
}
for(int i=;i<k-;i++)
{
cout<<b[i].id<<" ";
}
cout<<b[k-].id<<endl;
}
return ;
}
POJ3111(最大化平均值)的更多相关文章
- POJ-2976 Dropping tests---二分最大化平均值
题目链接: https://cn.vjudge.net/problem/POJ-2976 题目大意: 给定n个二元组(a,b),扔掉k个二元组,使得剩下的a元素之和与b元素之和的比率最大 解题思路: ...
- POJ_3111_K_Best_(二分,最大化平均值)
描述 http://poj.org/problem?id=3111 n个珠宝,第i个有价值v[i]和重量w[i],要从总选k个,使得这k个的(价值之和)/(重量之和)即平均价值最大,输出选中的珠宝编号 ...
- POJ 3111 K Best(最大化平均值)
题目链接:click here~~ [题目大意]有n个物品的重量和价值各自是Wi和Vi.从中选出K个物品使得单位重量的价值最大,输出物品的编号 [解题思路]:最大化平均值的经典.參见click her ...
- poj 3111 K Best 最大化平均值 二分思想
poj 3111 K Best 最大化平均值 二分思想 题目链接: http://poj.org/problem?id=3111 思路: 挑战程序竞赛书上讲的很好,下面的解释也基本来源于此书 设定条件 ...
- poj 2976(二分搜索+最大化平均值)
传送门:Problem 2976 参考资料: [1]:http://www.hankcs.com/program/cpp/poj-2976-dropping-tests-problem-solutio ...
- 二分算法的应用——最大化平均值 POJ 2976 Dropping tests
最大化平均值 有n个物品的重量和价值分别wi 和 vi.从中选出 k 个物品使得 单位重量 的价值最大. 限制条件: <= k <= n <= ^ <= w_i <= v ...
- 【二分查找-最大化平均值】POJ2976 - Dropping Test
[题目大意] 给出n组ai和bi,去掉k个使得a的总和除以b的总和最大. [思路] 也就是取(n-k)个数,最大化平均值,见<挑战程序设计竞赛>P144,最后公式为c(x)=((ai-x* ...
- POJ 2976 Dropping tests (最大化平均值)
题目链接:click here~~ [题目大意]给你n个分数的值,要求最小不选k个,使得最后分数相加结果平均值最大 [解题思路]:最大化平均值:參见:click here~~ 代码: #include ...
- NYOJ 914 Yougth的最大化【二分/最大化平均值模板/01分数规划】
914-Yougth的最大化 内存限制:64MB 时间限制:1000ms 特判: No 通过数:3 提交数:4 难度:4 题目描述: Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从 ...
随机推荐
- CentOS 6.5下Redmine的安装配置
首先引用百度介绍下redmine: Redmine是用Ruby开发的基于web的项目管理软件,是用ROR框架开发的一套跨平台项目管理系统,据说是源于Basecamp的ror版而来,支持多种数据库,有不 ...
- [Python]基于CNN的MNIST手写数字识别
目录 一.背景介绍 1.1 卷积神经网络 1.2 深度学习框架 1.3 MNIST 数据集 二.方法和原理 2.1 部署网络模型 (1)权重初始化 (2)卷积和池化 (3)搭建卷积层1 (4)搭建卷积 ...
- Apache Phoenix基本操作-2
1. 如何映射一个Phoenix的表到一个Hbase的表? 你可以通过Create table/create view DDL语句在一个已经存在的hbase表上创建一个Phoenix表或者视图.对于C ...
- linux防火墙开关
对于普通Linux机器开关防火墙命令: 1重启后生效 chkconfig iptables off chkconfig iptables on 2即时生效 serivce iptables statu ...
- JavaScript文件下载 兼容所有浏览器 不可跨域
前端文件下载 兼容所有浏览器 download.js文件下载,几乎支持所有类型下载,详细内容参考官网 http://danml.com/download.html 引入文件 <script sr ...
- 请求被中止: 未能创建 SSL/TLS 安全通道,以及解决方法,即:Could not create SSL/TLS secure channel
C# 访问https请求被中止: 未能创建 SSL/TLS 安全通道(Could not create SSL/TLS secure channel) 以及 X509Certificate2 temp ...
- 利用ChromeOptions()加载用户配置
一. 如何绕过页面登录 我们在登录网站的时候,通常需要输入用户名.密码和验证码,那么有没有办法绕过登录环节呢? 有两种方法可以解决这个问题,一种是利用chrome浏览器的用户配置,一种是利用cooki ...
- 《Advanced Bash-scripting Guide》学习(十九):两个整数的最大公约数
本文所选的例子来自于<Advanced Bash-scripting Gudie>一书,译者 杨春敏 黄毅 #!/bin/bash #求两个整数的最大公约数 E_BADARGS= #如果参 ...
- uva 10739 dp
https://vjudge.net/problem/UVA-10739 和昨天的那个回文串几乎一样只是加了条件限制,可以随意增删以及替换. #include<iostream> #inc ...
- uva 10168 哥德巴赫猜想
https://vjudge.net/problem/UVA-10168 给出一个整数n,问是否能将它化为四个素数相加的形式,如果可以的话就输出这四个数.显然n<8时是不可能的.对于大于等于8得 ...