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,你能帮他从 ...
随机推荐
- 20165101刘天野 2017-2018-2 《Java程序设计》第6周学习总结
#20165101刘天野 2017-2018-2 <Java程序设计>第6周学习总结 教材学习内容总结 第八章:常用实用类 String类:不可变类,一些看起来能够改变String的方法其 ...
- 图片的另一种展现—将后台图片编码直接展现为图片
1.应用场景 开发过程中,遇到这样的需求:需要将服务器上的图片展现在页面上,但是图片所在服务器不是对外的,图片所在服务器与应用服务器也不在同一台机器上,这时候就需要在开发中先将图 ...
- mysql启动报can't create/write to file 'var/run/mysqld/mysqld.pid 错误解决办法
msql启动报错,启动不了. 进入mysql日志默认的路径为 /var/log/mysqld.log 查看日志,发现报错信息如下: can't create/write to file 'var/ru ...
- 在eclipse中使用Github进行单元测试
时间分配是找资料60%,动手实践40%.因为之前一直没怎么好好学习,导致看到使用Gunit进行单元测试的时候感觉像是看到了一行符号一样不知所措,还好现在搜索引擎很强大,找资料学习中间有找到一个现成的傻 ...
- 使用标签代替goto关键字
众所周知,java中没有goto语句,但是保留了goto这个关键字.由于goto是在源码级上的跳转,多次使用goto会引起代码混乱容易出错,这也是java取消goto语句的目的所在,但是goto语句也 ...
- Oracle创建表空间和增加表空间
1.创建表空间 create tablespace fgq datafile 'E:\app\Administrator\oradata\fms\fgq01.dbf' size 1000M autoe ...
- 字典:dict.c/dict.h
Redis 源码分析(1):字典和哈希表(dict.c 和 dict.h)http://huangz.iteye.com/blog/1455808两个点:字典结构的运作流程哈希表的渐进式 rehash ...
- 去除sql中不可见字符的n种方法
CREATE TABLE [ASCII0127] ( Bin INT, Dec INT, Hex VARCHAR(128), Abbr ...
- Excel 导入到处问题处理!
1.未在本地计算机上注册"Microsoft.Jet.OLEDB.4.0" 因为没有安装64位的Jet40驱动.可以到 http://www.microsoft.com/downl ...
- 计算客网络赛 Coin 二项式定理+逆元
https://nanti.jisuanke.com/t/17115 Bob has a not even coin, every time he tosses the coin, the proba ...