# 题目链接

https://vjudge.net/problem/POJ-3111

# 题面

## 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 = $${i_1, i_2, …, i_k}$$ as
$s(S)=\frac{\sum_\limits{j=1}^kv_{i_j}}{\sum_\limits{j=1}^kw_{i_j}}$

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 ≤ kn ≤ 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

# 题解

$s(S)=\frac{\sum_\limits{j=1}^kv_{i_j}}{\sum_\limits{j=1}^kw_{i_j}}$

$s(S) \times \sum_\limits{j=1}^kw_{i_j}=\sum_\limits{j=1}^kv_{i_j}$

## AC代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#define N 100050
#define eps 1e-8
using namespace std;
typedef long long ll;
int n, k;
struct node {
double v, w, tmp;
int id;
} a[N];
bool cmp(node a, node b) {
return a.tmp > b.tmp;
}
bool check(double x) {
for (int i = 1; i <= n; i++) {
a[i].tmp = a[i].v - a[i].w * x;
}
sort(a + 1, a + n + 1, cmp);
double nowsum = 0;
for (int i = 1; i <= k; i++) {
nowsum += a[i].tmp;
}
return nowsum >= 0;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%lf%lf", &a[i].v, &a[i].w);
a[i].id = i;
}
double l = 0, r = 1e6;
double mid;
while(r - l > eps) {
mid = (l + r) / 2;
if (check(mid)) {
l = mid;
}
else r = mid;
}
for (int i = 1; i <= k; i++) {
printf("%d ", a[i].id);
}
return 0;
}


0条评论