天天看点

POJ 3111 K Best

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 1
1 2
1 3      

Sample Output

1 2

属于01分数规划的题,可以二分答案来搞定。需要注意精度问题。

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define fi first
#define se second
#define mp(i,j) make_pair(i,j)
#define pii pair<int,int>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-6;
const int INF = 0x7FFFFFFF;
const int mod = 9973;
const int N = 1e5 + 10;
int n, m;
int x[N], y[N], a[N];
double g[N];

bool cmp(int a, int b)
{
  return g[a] > g[b];
}

bool check(double z)
{
  rep(i, 1, n) g[i] = 1.0 * x[i] - z * y[i], a[i] = i;
  sort(a + 1, a + n + 1, cmp);
  double sum = 0;
  rep(i, 1, m) sum += g[a[i]];
  return sum >= 0;
}

int main()
{
  while (scanf("%d%d", &n, &m) != EOF)
  {
    double l = 0, r = 0;
    rep(i, 1, n) scanf("%d%d", &x[i], &y[i]), r = max(r, 1.0*x[i] / y[i]);
    r += 10;
    while (l + eps <= r)
    {
      double mid = (l + r) / 2;
      if (check(mid)) l = mid; else r = mid;
    }
    rep(i, 1, m) printf("%d%s", a[i], i == m ? "\n" : " ");
  }
  return 0;
}