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. 详细讲解安全升级MySQL的方法

    MySQL升级是非常必要的. 我们在Percona Support上列出了关于MySQL升级最佳实践的各种问题.这篇文章推荐了一些不同情况下升级MySQL的方法. 为什么MySQL升级是必须的? 原因 ...

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

    20155235 2016-2017-2 <Java程序设计>第六周学习总结 教材学习内容总结 第十章 InputStream与OutputStreaam 串流设计的概念 串流继承架构 川 ...

  3. 让老版本IE支持HTML5

    一直想入手C3和H5,但因为所开发的项目一直要求兼容IE7,IE8.而这两个浏览器并不支持html5,所以一直都在观望而未真正的投入太多精力去学习.尽管我知道h5和c3是主流. 在最近的项目开发中,偶 ...

  4. CSS基础之选择器

    一:CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到,一个样式表时,就会按照. 二:CSS语法 每个CSS有两部分组成:选择器和声明 ...

  5. Python数据类型(整型,字符串类型,列表)

    一:数据的概念 1.数据是什么 x=10,数据10就是我们要存储的数据. 2.为什么数据要分不同的种类? 因为数据是用来表示状态的,不同的状态就要用不同类型的数据去表示. 3:Python中常见的数据 ...

  6. nginx安装Lets Encrypt SSL免费HTTPS加密证书

    Linux Nginx网站:Certbot安装配置Lets Encrypt SSL免费HTTPS加密证书 原文地址:https://renwole.com/archives/157 实验环境:Cent ...

  7. Python——杂记

    python 最近出错总结: 1.而for..in ..中不要用else if  x in y:     print  else:     print2.def fibs(num): ...     ...

  8. 查看linux系统的信息

    #!/bin/sh ################################################## #function:get host's information #Autho ...

  9. Android Build.VERSION.SDK_INT兼容介绍

    尽管Android向下兼容不好,但是一个程序还是可以在多个平台上跑的.向下兼容不好,接口改变,新的平台上不能用旧的API,旧的平台更不可能用新的API,不等于一个平台需要一个APK.可以在高SDK上开 ...

  10. Linux Kernel sys_call_table、Kernel Symbols Export Table Generation Principle、Difference Between System Calls Entrance In 32bit、64bit Linux【转】

    转自:http://www.cnblogs.com/LittleHann/p/4127096.html 目录 1. sys_call_table:系统调用表 2. 内核符号导出表:Kernel-Sym ...