POJ 3111 K Best(01分数规划)

K Best
Time Limit: 8000MS   Memory Limit: 65536K
Total Submissions: 9876   Accepted: 2535
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 = {i1i2, …,ik} as

POJ 3111 K Best(01分数规划).

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

Source

题目链接:POJ 3111

这题多了一个得输出方案序号,用结构体记录一下id就行,代码的话迭代或者二分都可以,但是迭代速度快很多很多

迭代代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 100010;
const double eps = 1e-9;
struct info
{
double w, v, d;
int id;
bool operator<(const info &rhs)const
{
return d > rhs.d;
}
};
info A[N]; double getnewk(int n, int k, double r)
{
double V = 0.0, W = 0.0;
for (int i = 0; i < n; ++i)
A[i].d = A[i].v - r * A[i].w;
sort(A, A + n);
for (int i = 0; i < k; ++i)
{
V += A[i].v;
W += A[i].w;
}
return V / W;
}
int main(void)
{
int n, k, i;
while (~scanf("%d%d", &n, &k))
{
for (i = 0; i < n; ++i)
{
scanf("%lf%lf", &A[i].v, &A[i].w);
A[i].id = i + 1;
}
double ans = 1, temp = 1;
while (1)
{
temp = getnewk(n, k, ans);
if (fabs(temp - ans) < eps)
break;
ans = temp;
}
for (i = 0; i < k; ++i)
printf("%d%s", A[i].id, i == k - 1 ? "\n" : " ");
}
return 0;
}
上一篇:ElasticSearch集群设置


下一篇:POJ 3111 K Best(二分答案)