K Best POJ
来源:互联网 发布:公路基础数据库系统 编辑:程序博客网 时间:2024/06/07 10:14
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 ≤ 10^6, 1 ≤ wi ≤ 10^6, both the sum of all vi and the sum of all wi do not exceed 10^7).
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
Hint
题意
n件物品 每件物品价值v重量w
问选择k件有最大平均值
题解:
二分枚举答案
判断
每个物品v减去这个值乘上w 再对前k个求和看是否大于等于0 说明这个值小于等于结果 左区间等于mid
代码
#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <algorithm>using namespace std;const int N = 1e5+100;const double eps = 1e-6;int v[N];int w[N];int a[N];double y[N];int n,k;/*按照a在y[i]中的大小排a[i]的序*/bool cmp(int a,int b){ return y[a]>y[b];}bool check(double x){ for (int i = 1;i <= n; ++i){ a[i] = i; y[i] = 1.0*v[i]-x*w[i]; } sort(a+1,a+n+1,cmp); double sum = 0; for (int i = 1; i <= k; ++i){ sum += y[a[i]]; } if (sum>=0) return true; return false;} int main(){ scanf("%d%d",&n,&k); double r = 0; for (int i = 1; i <= n; ++i){ scanf("%d%d",&v[i],&w[i]); r = max(r,v[i]*1.0/w[i]); } r+=10; double l = 0; while (r-l>=eps){ double mid = (l+r)/2; if (check(mid)) l = mid; else r = mid; } for (int i = 1;i <= k; ++i){ printf("%d%c",a[i],i==k?'\n':' '); } return 0;}
- POJ 3111 K Best
- poj 3111 K Best
- POJ-3111K Best
- poj 3111 K Best
- POJ 3111K Best
- K Best POJ
- K Best POJ
- POJ 3111 K Best 笔记
- poj 3111 K Best 二分
- POJ 3111 K Best 最大化平均值
- POJ 3111 - K Best(二分搜索)
- poj K Best 最大化平均值 二分搜索
- POJ 3111 K Best(最大化平均值)
- POJ 3111 K Best(最大化平均值)
- POJ 3111 K Best <最大化平均,二分>
- poj 3111 K Best(二分)
- POJ 3111 K Best 【二分:最大化平均值】
- POJ 3111 K Best 二分 最大化平均值
- SDUT-顺序表应用6:有序顺序表查询
- I
- 腾讯开源ncnn:caffemodel转化为.param和.bin文件
- 2017第一次多校联合 1001Add More Zero
- MNIST机器学习入门
- K Best POJ
- 欢迎使用CSDN-markdown编辑器
- 关于NoSQL与SQL的区别
- Android--方法和封装类记录
- IPMIversion2.0学习笔记1
- ios-屏幕适配
- NAT模式静态指定虚拟机的ip地址方法
- 2017-07-25 51nod 1119 组合数取模 某数关于质数的逆元
- nyoj283对称排序