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,你能帮他从 ...
随机推荐
- 如何在windows10环境下安装Pytorch-0.4.1版本
开始是按照教程:https://blog.csdn.net/xiangxianghehe/article/details/80103095 安装了Pytorch0.4.0,但是安装后发现在import ...
- 数据结构与算法之美 06 | 链表(上)-如何实现LRU缓存淘汰算法
常见的缓存淘汰策略: 先进先出 FIFO 最少使用LFU(Least Frequently Used) 最近最少使用 LRU(Least Recently Used) 链表定义: 链表也是线性表的一种 ...
- Linux之Xinetd服务介绍
一.概念:1.独立启动的守护进程:stand-alone,每个特定服务都有单独的守护进程,这个处理单一服务的始终存在的进程就是独立启动的守护进程. 2.超级守护进程:多个服务统一由一个进程管理,该进程 ...
- springcloud一些概念知识
1.Eureka 1)Eureka服务治理体系支持跨平台 2)三个核心概念:服务注册中心.服务提供者以及服务消费者 3)服务续约:注册完服务之后,服务提供者会维护一个心跳来不停的告诉Eureka Se ...
- 探究操作系统【TLCL】
ls – List directory contents file – Determine file type less – View file contents ls常用选项 ls -a 全部输出 ...
- MapReduce-二进制输入
Hadoop的MapReduce不只是可以处理文本信息,它还可以处理二进制格式的数据1. 关于SequenceFileInputFormat类Hadoop的顺序文件格式存储二进制的键/值对的序列.由于 ...
- Visual Studio中用于ASP.NET Web项目的Web服务器
当您在 Visual Studio 中开发 Web 项目时,需要 Web 服务器才能测试或运行它们. 利用 Visual Studio,您可以使用不同的 Web 服务器进行测试,包括 IIS Expr ...
- Win7使用之查端口,杀进程
对 Win7 的 cmd 使用不熟练,这里记录下经常用到的命令吧! 查看所有端口占用情况:netstat -ano 查看指定端口占用情况:netstat -ano | findstr "80 ...
- Redis简介 & 与Memcache的区别
redis 是一个基于内存的高性能key-value数据库. Reids的特点 Redis本质上是一个Key-Value类型的内存数据库,很像memcached,整个数据库统统加载在内存当中进行操 ...
- spring:自定义限定符注解@interface, 首选bean
spring:自定义限定符注解@interface, 首选bean 1.首选bean 在声明bean的时候,通过将其中一个可选的bean设置为首选(primary)bean能够避免自动装配时的歧义性. ...