http://poj.org/problem?id=3111
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
、
题目大意:给出
n
n
n个珠宝的价值和重量,你可以从中任意选出
k
k
k个,使这
k
k
k个的单位价值最大。
思路:不妨设: ∑ i = 1 k v [ i ] / ∑ i = 1 k w [ i ] = x \sum_{i=1}^{k}v[i]/\sum_{i=1}^{k}w[i]=x ∑i=1kv[i]/∑i=1kw[i]=x,那么就是在满足: ∑ i = 1 k v [ i ] − ∑ i = 1 k w [ i ] ∗ x > = 0 \sum_{i=1}^{k}v[i]-\sum_{i=1}^{k}w[i]*x>=0 ∑i=1kv[i]−∑i=1kw[i]∗x>=0的情况下找 x x x的最大值,而这个最大值可以通过二分来找。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define INF 0x3f3f3f3f
#define eps 1e-6
using namespace std;
const int maxn=1e5+5;
struct node
{
double v;
int id;
bool operator < (const node &a)const
{
return v>a.v;
}
}a[maxn];
int n,k;
int w[maxn],v[maxn];
bool check(double mid)
{
for(int i=0;i<n;i++)
a[i].id=i+1,a[i].v=v[i]-mid*w[i];
sort(a,a+n);
double sum=0;
for(int i=0;i<k;i++)
sum+=a[i].v;
return sum>=0;
}
int main()
{
while(~scanf("%d%d",&n,&k))
{
for(int i=0;i<n;i++)
scanf("%d%d",&v[i],&w[i]);
double l=0.0,r=INF,mid;
while(r-l>eps)
{
mid=(l+r)/2;
if(check(mid))
l=mid;
else
r=mid;
}
for(int i=0;i<k;i++)
printf("%d%c",a[i].id,i==k-1?'\n':' ');
}
return 0;
}
本文介绍了一种解决珠宝选择问题的算法,旨在从n个珠宝中选择k个,使得这些珠宝的单位价值最大化。通过设定单位价值公式,并利用二分查找法找到最优解,最后输出选定的珠宝编号。

477

被折叠的 条评论
为什么被折叠?



