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.
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 k numbers — the numbers of jewels Demy must keep. If there are several solutions, output any one.
3 2 1 1 1 2 1 3
1 2
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define inf 1e6+1
#define eps 1e-7
const int maxn = 1e5+5;
int n, k, t;//t代表下标
struct node{
int temp; //物品的编码
double v, w;
double divide;//即v[i]-K*w[i] 的值
}s[maxn];
bool cmp(node a, node b) { //排序
return a.divide > b.divide;
}
int ans[maxn]; //存选择的物品
int judge(double K) {
t = 0;
double sum = 0;
for(int i = 1; i <= k; i++) {
s[i].divide = s[i].v-K*s[i].w;
}
sort(s+1, s+k+1, cmp);
for(int i = 1; i <= n; i++) {
sum += s[i].divide; //求和
ans[t++] = s[i].temp; //将选择的物品存入数组
}
if(sum >= 0) return 1;
return 0;
}
int main() {
scanf("%d %d", &k, &n);
for(int i = 1; i <= k; i++) {
s[i].temp = i;
scanf("%lf %lf", &s[i].v, &s[i].w);
}
double left = 0, right = inf, mid;
while(right - left > eps) {
mid = (left + right)/2;
if(judge(mid)) left = mid;
else right = mid;
}
for(int i = 0; i < n; i++) {
printf("%d", ans[i]);
if(i != n-1) printf(" ");
}
return 0;
}