K Best

Time Limit: 8000MS Memory Limit: 65536K
Case 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 = {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 2
1 1
1 2
1 3

Sample Output

1 2

思路:

二分中最大化平均值的模板题,主要的就是找到要输出的那个最大化的值,在找寻的途中,要使值越大,那么分子则越大,分母则越小,比如(a + b) / (c + d) = e,而e是需要求出来的值,这个时候(a + b = e * c + e * d),所以有(a - e * c + b - e * d)是否大于零,大于的话,肯定e就越大,所以要使(a - e * c)这样的越大,就将这些存到数组,然后排序。这一题的话v和w没有v > w之类的关系,所以在二分的时候要从最小和最大开始找起,还有就是,这一题最好用友元类去写排序,用bool cmp的话会超时。而这题最后输出的是保存的珠宝的编号,所以就要用个结构体去存编号,最后输出,因为答案不唯一,所以不用按照顺寻之类的有编号对就行了,不用考虑顺序。

#include <iostream>
#include <algorithm>
using namespace std;
const double eps = 1e-5;
const int maxn = 1e+6 + 10;
struct NODE {
	int id;
	double y;
	bool friend operator < (NODE a, NODE b) {
		return a.y > b.y;
	}
};
int n, k;
NODE node[maxn];
int v[maxn], w[maxn];
int check(double x) {
	double sum = 0;
	for (int i = 0; i < n; i++) {
		node[i].y = v[i] - w[i] * x;
		node[i].id = i + 1;
	} 
	sort(node, node + n);
	for (int i = 0; i < k; i++) {
		sum += node[i].y;
	}
	return sum;
}
int main() {
	scanf("%d %d", &n, &k);
	for (int i = 0; i < n; i++) {
		scanf("%d %d", &v[i], &w[i]);
	}
	double l = 0, r = 0x7fffffff;
	while (r - l > eps) {
		double mid = (l + r) / 2;
		if (check(mid) < 0) r = mid;
		else l = mid;
	}
	for (int i = 0; i < k; i++) {
		if (i != 0) cout << " ";
		cout << node[i].id;	
	}
	return 0;
}