POJ3111-K Best

来源:互联网 发布:人工智能产业园区规划 编辑:程序博客网 时间:2024/06/05 02:38

K Best
Time Limit: 8000MS Memory Limit: 65536KTotal Submissions: 10263 Accepted: 2644Case 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 = {i1i2, …, 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;}

原创粉丝点击