poj3111 K Best【最大化平均值】
来源:互联网 发布:中韩贸易逆差数据 编辑:程序博客网 时间:2024/05/16 06:18
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 21 11 21 3
Sample Output
1 2思路:二分解决最大化平均值,玄学的是C++超时,G++过了;
二分的是最后的平均值,将题目中式子变化一下就是 s[i].d = s[i].a - mid * s[i].b ;用out数组记录 i 就可以了;
#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define max_n 100010using namespace std;typedef long long LL;double in[max_n];int out[max_n];int n,k;struct node{double a;double b;int c;double d;}s[max_n];bool cmp(node x,node y){return x.d>y.d;}bool judge(double mid){for(int i=0;i<n;i++){s[i].d=s[i].a-mid*s[i].b;}sort(s,s+n,cmp);double sum=0.0;for(int i=0;i<k;i++){sum+=s[i].d;out[i]=s[i].c;}return sum>=0.0;}int main(){while(~scanf("%d %d",&n,&k)){memset(s,0,sizeof(s));memset(out,0,sizeof(out));for(int i=0;i<n;i++){scanf("%lf %lf",&s[i].a,&s[i].b);s[i].c=i+1;}double l=0.0,r=10000000.0,mid;while(r-l>1e-6){mid=(l+r)/2.0;if(judge(mid)) l=mid;else r=mid;}//printf("%lf\n",l);for(int i=0;i<k;i++){if(i>0) printf(" ");printf("%d",out[i]); } printf("\n");}return 0;}
- POJ3111-K Best-最大化平均值
- poj3111 K Best【最大化平均值】
- POJ3111 K Best 最大化平均值(二分)
- POJ3111 K Best(二分 最大化平均值)
- poj3111 二分最大化平均值
- POJ3111 最大化平均值
- POJ 3111 K Best 最大化平均值
- poj K Best 最大化平均值 二分搜索
- POJ 3111 K Best(最大化平均值)
- POJ 3111 K Best(最大化平均值)
- POJ 3111 K Best 【二分:最大化平均值】
- POJ 3111 K Best 二分 最大化平均值
- POJ3111--K Best
- poj3111 K Best
- POJ3111-K Best
- POJ3111 K Best
- POJ3111-K Best
- K Best poj3111
- php+jquery+ajax实现用户名验证
- postgresql里cmin与cmax有何不同.md
- RSI指标的原理和计算方法
- 线性表的基本操作 C++
- Oracle 只对部分行建索引
- poj3111 K Best【最大化平均值】
- keras的使用
- 我所理解的js面向对象
- Android持续构建
- 解决webstorem卡顿问题
- Application属性详解
- For input string: "${JDBC.MinPoolSize}" spring加载jdbc.properties配置文件报错
- 教大家如何使用Eclipse进行远程调试
- 如何将自己的工程上传到github!