K Best
Time Limit: 8000MS   Memory Limit: 65536K
Total Submissions: 12812   Accepted: 3290
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 = {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

Source

Northeastern Europe 2005, Northern Subregion
题目链接 点击打开链接

题目大意:给你N个珠宝已经每个珠宝的价值和重量,取其中K个。问取哪几个珠宝使得sigma(v[i])/ sigma(w[i])最大。
思路:构造函数 sigma(t[i]) = sigma(v[i]) - sigma(w[i]) * x。 然后二分x值。   方法为0-1分数规划。

#include<stdio.h>
#include<algorithm>
using namespace std; #define exp 1e-8
struct jew{
int id;
double y;
}num[1000005];
double v[1000005], w[1000005];
bool cmp(jew a, jew b)
{
return a.y > b.y;
} bool dis(double x, int n, int k)
{
int i;
double sum = 0;
for (i = 0; i < n; i++)
{
num[i].y = v[i] - x*w[i];
num[i].id = i + 1;
}
sort(num, num + n, cmp);
for (i = 0; i < k; i++)
{
sum += num[i].y;
}
return sum >= 0;
}
int main()
{
int n, k;
while (~scanf("%d %d", &n, &k))
{
for (int i = 0; i < n; i++)
{
scanf("%lf %lf", &v[i], &w[i]);
}
double left = 0, right = 1e6;
double mid;
while (right - left>=exp)
{
mid = (left + right) / 2;
if (dis(mid, n, k))
{
left = mid;
}
else
{
right = mid;
}
}
for (int i = 0; i < k - 1; i++)
{
printf("%d ", num[i].id);
}
printf("%d\n", num[k - 1].id);
} return 0;
}

POJ - 3111 K Best 0-1分数规划 二分的更多相关文章

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

    K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 9876   Accepted: 2535 Case Time ...

  2. POJ - 2976 Dropping tests && 0/1 分数规划

    POJ - 2976 Dropping tests 你有 \(n\) 次考试成绩, 定义考试平均成绩为 \[\frac{\sum_{i = 1}^{n} a_{i}}{\sum_{i = 1}^{n} ...

  3. poj 2976 Dropping tests 0/1分数规划

    0/1分数规划问题,用二分解决!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> # ...

  4. POJ 2976 Dropping tests 【01分数规划+二分】

    题目链接:http://poj.org/problem?id=2976 Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  5. LOJ149 0/1分数规划

    竟然没有写过分数规划的题解 考前挣扎一发板子( 二分答案k 然后0/1分数规划的方法就是 分母乘过去然后贪心解决 注意实数二分的精度 一般估计一个次数比较好不然容易出现精度比较误差[惨痛教训 就做完了 ...

  6. POJ 2976 Dropping tests【0/1分数规划模板】

    传送门:http://poj.org/problem?id=2976 题意:给出组和,去掉对数据,使得的总和除以的总和最大. 思路:0/1分数规划 设,则(其中等于0或1) 开始假设使得上式成立,将从 ...

  7. POJ2976 题解 0/1分数规划入门题 二分

    题目链接:http://poj.org/problem?id=2976 关于 0/1分数规划 参见 这篇博客 实现代码如下: #include <cstdio> #include < ...

  8. poj 3111 K Best 最大化平均值 二分思想

    poj 3111 K Best 最大化平均值 二分思想 题目链接: http://poj.org/problem?id=3111 思路: 挑战程序竞赛书上讲的很好,下面的解释也基本来源于此书 设定条件 ...

  9. LOJ 3089 「BJOI2019」奥术神杖——AC自动机DP+0/1分数规划

    题目:https://loj.ac/problem/3089 没想到把根号之类的求对数变成算数平均值.写了个只能得15分的暴力. #include<cstdio> #include< ...

  10. Bzoj1486/洛谷P3199 最小圈(0/1分数规划+spfa)/(动态规划+结论)

    题面 Bzoj 洛谷 题解(0/1分数规划+spfa) 考虑\(0/1\)分数规划,设当前枚举到的答案为\(ans\) 则我们要使(其中\(\forall b_i=1\)) \[ \frac{\sum ...

随机推荐

  1. why inline functions must be put in header files?

    [why inline functions must be put in header files?] 编译中有2个过程:compile.link.先进行compile,compile中把源代码编译成 ...

  2. 20155306 2016-2017-2 《Java程序设计》第七周学习总结

    20155306 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 第十三章 时间与日期 三种时间: 格林威治标准时间(GMT)的正午是太阳抵达天空最高点之时, ...

  3. HDU 5701 中位数计数 (思维题)

    题目链接 Problem Description 中位数定义为所有值从小到大排序后排在正中间的那个数,如果值有偶数个,通常取最中间的两个数值的平均数作为中位数. 现在有n个数,每个数都是独一无二的,求 ...

  4. Android笔记之开机自启

    有时候需要应用具有开机自启的能力,或者更常见的场景是开机时悄悄在后台启动一个Service. 关键点: 1. Android系统在开机的时候会发送一条广播消息,只需要接收这条广播消息即可,不过需要注意 ...

  5. 32、TreeSet简介

    使用TreeSet存储Integer对象 TreeSet的特点是可以对存放进去的元素进行排序. package com.sutaoyu.list; import java.util.TreeSet; ...

  6. GreenTrend

    ExpertforSQLServer(4.7.2)和ZhuanCloud(1.0.0)工具收集内容(在个人笔记本上测试) --SZC_Info.txt :: SQL专家云 v1. :: 开始收集 :: ...

  7. windows 10 部署flask web

    起因 本来想这用django 写一个web 应用程序,便于管理mongodb的数据.结果django 不直接支持mongodb……又没时间研究NoSQL.于是想着随便整个api吧…… 于是先用flas ...

  8. pywin32记录备忘

    项目地址:http://sourceforge.net/projects/pywin32/ 文档地址:http://docs.activestate.com/activepython/2.7/pywi ...

  9. Dream------scala--函数定义、流程控制、异常处理

    Dream------scala--函数定义.流程控制.异常处理 一.函数的定义 1.新建工程

  10. 【技巧总结】Penetration Test Engineer[4]-Web-Security(文件处理、会话管理、访问控制、SSRF、反序列化漏洞)

    Web安全基础2 3.8.文件处理 1)文件上传 一个正常的业务需求,问题在于控制上传合法文件. 防御文件上传 客户端javascript校验(通常校验扩展名) 检查MIME类型 检查内容是否合法 随 ...