POJ3111
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
题解:
AC代码为:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <set> using namespace std; const int maxn = + ;
const double inf = + ;
int n,k;
struct jew
{
int id;
int v,w;
double key;
void cal(double x)
{
key = v - x * w;
} bool operator < (const jew& a) const
{
return key > a.key;
}
}j[maxn]; bool c(double x)
{
for (int i = ; i <= n; i++)
{
j[i].cal(x);
} sort(j+,j+n+); double tmp = ;
for (int i = ; i <= k; i++)
tmp += j[i].key;
return tmp >= ;
} int main()
{
while (cin>>n>>k)
{
for (int i = ; i <= n; i++)
{
scanf("%d%d",&j[i].v,&j[i].w);
j[i].id = i;
}
double l = , r = inf;
for (int i = ; i < ; i++)
{
double mid = (l + r) / ;
if (c(mid))
l = mid;
else
r = mid;
}
for (int i = ; i <= k; i++)
{
if (i - )
printf(" %d",j[i].id);
else
printf("%d",j[i].id);
} cout<<endl;
}
}
最新文章
- 微信小程序demo汇总
- DP专题训练之HDU 1087 	Super Jumping!
- 获取layer.open弹出层的返回值
- Problem A CodeForces 556A
- shell local
- 在 eclipse 中设置每行的字数
- 使用sql生成UUID
- [Angular 2] Directive intro and exportAs
- PLSQL Package dubug方法
- js的数组操作
- NM_CUSTOMDRAW 消息
- 用C#代码实现类似QQ窗体的“上、左、右”停靠功能
- JavaScript 再谈闭包
- LoadRunner(四)——深度了解LR相关功能
- coreData的ManagedObject后,报错
- eclipse代码自动补全。
- iOS: 数据持久化方案
- 物联网架构成长之路(4)-EMQ插件创建
- 通过图片获取gps地理位置
- 10 个理由让你继续干 IT