POJ3111-K Best
来源:互联网 发布:人工智能产业园区规划 编辑:程序博客网 时间:2024/06/05 02:38
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
Source
题意:有n件珠宝,每样珠宝有两个属性,价值v和重量w。选择k件,使得k件的价值和除以k件的重量和最大,也就是单位重量的价值最大
解题思路:二分比值,和POJ2976-Dropping tests一样
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <cmath>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;int n,k;struct node{int a, b,id;double p;}x[100009];bool cmp(node a, node b){return a.p > b.p;}bool check(double xx){for (int i = 1; i <= n; i++) x[i].p = 1.0*x[i].a -1.0* x[i].b*xx;sort(x + 1, x + 1 + n, cmp);double sum1 = 0, sum2 = 0;for (int i = 1; i <= k; i++) sum1 += x[i].a, sum2 += x[i].b;return sum1 / sum2 > xx;}int main(){while (~scanf("%d%d", &n, &k)){for (int i = 1; i <= n; i++) scanf("%d%d", &x[i].a, &x[i].b), x[i].id = i;double l = 0, r = 1.0*INF;while (fabs(r - l) > 1e-8){double mid = (l + r) / 2;if (check(mid)) l = mid;else r = mid;}printf("%d", x[1].id);for (int i = 2; i <= k; i++) printf(" %d", x[i].id);printf("\n");}return 0;}
- POJ3111--K Best
- poj3111 K Best
- POJ3111-K Best
- POJ3111 K Best
- POJ3111-K Best
- K Best poj3111
- poj3111-K Best
- POJ3111 K Best 二分搜索
- POJ3111-K Best-最大化平均值
- poj3111 K Best【最大化平均值】
- POJ3111 K Best 最大化平均值(二分)
- POJ3111 K Best(二分 最大化平均值)
- 求改poj3111 K Best二分
- poj3111- POJ - 3111 K Best (二分 + 01分数规划)
- poj3111
- poj3111
- poj3111
- POJ3111
- 自定义Toast(悬浮窗口)
- 从printf谈可变参数函数的实现[转载]
- 多源最短路径--floyd算法
- 1110. Complete Binary Tree (25)[完全二叉树]
- STL algorithm
- POJ3111-K Best
- 奇偶剪枝
- 数据库SQL语句查询及多表查询
- java web项目发布到linux服务器上运行出现乱码
- HashMap的内部实现机制
- Git和Github简单教程
- NodeMCU文档中文翻译 6 MQTT模块
- Oracle如何在字段右边补位
- 计算从根节点到特定子节点的路径 java