POJ_3111_K_Best_(二分,最大化平均值)
描述
http://poj.org/problem?id=3111
n个珠宝,第i个有价值v[i]和重量w[i],要从总选k个,使得这k个的(价值之和)/(重量之和)即平均价值最大,输出选中的珠宝编号.
| Time Limit: 8000MS | Memory Limit: 65536K | |
| Total Submissions: 8189 | Accepted: 2087 | |
| 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
Source
分析
二分.
最大化平均值(同POJ 2976 Dropping Tests).
不等式变形做就可以了.
注意:
1.算c的值的时候不用担心算爆,int会先被提升为double再做运算,所以r取INF也没关系,不会爆(最多是INF*INF).
2.但是r取INF精度就不够了,所以还是乖乖取max(a[i]/b[i])把...
3.排序的时候直接排成从大到小的,方便.
4.这题二分次数少了进度不够,次数多了超时.
#include<cstdio>
#include<algorithm>
using std :: sort;
using std :: max; const int maxn=,INF=0x7fffffff;
int n,k;
int ans[maxn];
struct point
{
int a,b,num;
double c;
}j[maxn];
double x,y; bool comp(point x,point y) { return x.c>y.c; } bool C(double x)
{
for(int i=;i<=n;i++) j[i].c=j[i].a-j[i].b*x;
sort(j+,j+n+,comp);
double sum=;
for(int i=;i<=k;i++) sum+=j[i].c;
return sum>=;
} void solve()
{
for(int i=;i<;i++)
{
double m=x+(y-x)/;
if(C(m))
{
x=m;
for(int i=;i<=k;i++) ans[i]=j[i].num;
}
else y=m;
}
for(int i=;i<k;i++) printf("%d ",ans[i]);
printf("%d",ans[k]);
} void init()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%d%d",&j[i].a,&j[i].b);
j[i].num=i;
y=max(y,(double)j[i].a/(double)j[i].b);
}
} int main()
{
freopen("k.in","r",stdin);
freopen("k.out","w",stdout);
init();
solve();
fclose(stdin);
fclose(stdout);
return ;
}
POJ_3111_K_Best_(二分,最大化平均值)的更多相关文章
- NYOJ 914 Yougth的最大化【二分/最大化平均值模板/01分数规划】
914-Yougth的最大化 内存限制:64MB 时间限制:1000ms 特判: No 通过数:3 提交数:4 难度:4 题目描述: Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从 ...
- POJ - 2976 Dropping tests(01分数规划---二分(最大化平均值))
题意:有n组ai和bi,要求去掉k组,使下式值最大. 分析: 1.此题是典型的01分数规划. 01分数规划:给定两个数组,a[i]表示选取i的可以得到的价值,b[i]表示选取i的代价.x[i]=1代表 ...
- POJ:2976 Dropping tests(二分+最大化平均值)
Description In a certain course, you take n tests. If you get ai out of bi questions correct on test ...
- POJ 2976 Dropping tests【二分 最大化平均值】
题意:定义最大平均分为 (a1+a2+a3+---+an)/(b1+b2+---+bn),求任意去除k场考试的最大平均成绩 和挑战程序设计上面的最大化平均值的例子一样 判断是否存在x满足条件 (a1+ ...
- POJ 2976 3111(二分-最大化平均值)
POJ 2976 题意 给n组数据ai,bi,定义累计平均值为: 现给出一个整数k,要求从这n个数中去掉k个数后,最大累计平均值能有多大?(四舍五入到整数) 思路 取n−k个数,使得累计平均值最大. ...
- poj 2976(二分搜索+最大化平均值)
传送门:Problem 2976 参考资料: [1]:http://www.hankcs.com/program/cpp/poj-2976-dropping-tests-problem-solutio ...
- poj 3111 K Best 最大化平均值 二分思想
poj 3111 K Best 最大化平均值 二分思想 题目链接: http://poj.org/problem?id=3111 思路: 挑战程序竞赛书上讲的很好,下面的解释也基本来源于此书 设定条件 ...
- 二分算法的应用——最大化平均值 POJ 2976 Dropping tests
最大化平均值 有n个物品的重量和价值分别wi 和 vi.从中选出 k 个物品使得 单位重量 的价值最大. 限制条件: <= k <= n <= ^ <= w_i <= v ...
- POJ 3111 K Best 最大化平均值 [二分]
1.题意:给一共N个物品,每个物品有重量W,价值V,要你选出K个出来,使得他们的平均单位重量的价值最高 2.分析:题意为最大化平均值问题,由于每个物品的重量不同所以无法直接按单位价值贪心,但是目标值有 ...
随机推荐
- c#调用c++ dll(一)
首先来说说c++中的dll 核心的一些知识 比较大的应用程序都由很多模块组成,这些模块分别完成相对独立的功能,它们彼此协作来完成整个软件系统的工作.可能存在一些模块的功能较为通用,在构造其它软件系统时 ...
- SQL的update from 理解
学习了sql的语句都有快3年,工作上使用都一年半的,最近突然想起update from语句,感觉好像很模糊,虽然语法上使用一直正确,一直都是这样使用,但是就好像不是很明白里面的深处意思. 今天特意测试 ...
- UIPanGestureRecognizer的使用
UIGestureRecognizer是一个定义基本手势的抽象类,具体什么手势,在以下子类中包含: 1.拍击UITapGestureRecognizer (任意次数的拍击) 2.向里或向外捏 ...
- C#中2、8、16进制 有符号转换10进制正负数
曾经让我苦想的其他进制转有符号整型问题,结果自己想到方法解决后才发现原来如此简单. 1.Int16(2个byte长度 ) : 方法 :Convert.ToInt16(进制编码,进制) a.16进制转1 ...
- 使用GitHub For Windows托管Visual Studio项目
本文写得比较早,更新的在VS上使用GitHub的文章请移步:Visual Stuido 2015 Community 使用 GitHub 插件 因为最近同时再看很多技术方面的书,书上的例子有很多自己想 ...
- bootstrap实现手风琴功能(树形列表)
首先把架包拷进项目,然后在页面中引进css,js <script src="js/jquery/jquery-2.1.1.min.js"></script> ...
- Java中ArrayList源码分析
一.简介 ArrayList是一个数组队列,相当于动态数组.每个ArrayList实例都有自己的容量,该容量至少和所存储数据的个数一样大小,在每次添加数据时,它会使用ensureCapacity()保 ...
- centOS 6.4 vsftpd 配置
###########配置流程########### 1 新建一个ftp用户,为了跟vsftp的虚拟用户对应 #useradd -d /home/vftpuser -s /sbin/nologi ...
- leetcode problem (5) Longest Palindromic Substring
最长回文子串: 1. 暴力搜索 时间复杂度O(n^3) 2. 动态规划 dp[i][j] 表示子串s[i…j]是否是回文 初始化:dp[i][i] = true (0 <= i <= ...
- datatable的数据转置
没有具体测试过,5w条数据在i5机器上大概是1.3~2s左右,但是个人感觉就是在处理数据库的分页或者是写条件的时候会有一些麻烦,不如使用数据库分页! 但是这种方法不失为一种思路 private voi ...