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.分析:题意为最大化平均值问题,由于每个物品的重量不同所以无法直接按单位价值贪心,但是目标值有 ...
随机推荐
- Oracle之Merge用法
Merge用来从一个表中选择一些数据更新或者插入到另一个表中.而最终是用更新还是用插入的方式取决于该语句中的条件. 下面我们简单的举一个例子: SQL)) 表已创建. SQL)) 表已创建. SQL, ...
- iOS9的那些坑 — — WeiboSDK registerApp启动就崩溃
在升级Xcode7.2.1后,在App加载时直接崩掉,仔细看了,发现是在在注册微博SDK的时候报错: [WeiboSDK registerApp:WBAPPKey]; 查了很多资料,最后在github ...
- LigerUI API
参数列表 参数名 类型 描述 默认值 title String 表格标题 null width String|Int 宽度值,支持百分比 'auto' height String|Int 高度值,支持 ...
- 九度OJ 1361 翻转单词顺序
题目地址:http://ac.jobdu.com/problem.php?pid=1361 题目描述: JOBDU最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Ca ...
- apache配置文件中的项目
对于每个配置项目,有几个要素: 首先是项目名称 其次是配置的语法 再次是配置的默认值 配置所处的配置文件的位置(分区) 配置所在的模块分区(和核心是否紧密) 配置项目所在的模块 所以对于每个配置项目, ...
- sort对象数组排序
function objectSort(property, desc) { //降序排列 if (desc) { return function (a, b) { return (a[property ...
- php怎么保留相除后几位小数:sprintf
$n=0.1265489; echo sprintf("%.2f", $n); // 0.13
- js 验证输入框金额
$("#ipt1").keyup(function () { var reg = $(this).val().match(/\d+\.?\d{0,2}/); var txt = ' ...
- 在Linux下不使用密码远程登陆其他Linux
有时需要再一台Linux上登陆其他Linux服务器,通常可以直接使用SSH命令,加入两台服务器一台服务器A,IP地址192.168.1.2,另一台服务器B,IP地址192.168.1.3,如果想从A服 ...
- 回答了个问题,9x9 乘法表生成器
# -*- coding: utf-8 -*- from prettytable import PrettyTable pt = PrettyTable() # 需要安装prettytable这个库来 ...