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. 【译】SSH隧道:本地和远程端口转发

    本文是:SSH Tunnel - Local and Remote Port Forwarding Explained With Examples 的译文 有两种方法可以创建SSH隧道,本地和远程端口 ...

  2. Android笔记之广播

    为了容易理解,可以将广播代入到事件模型中,发送广播消息看做是触发event,BroadcastReceiver是处理事件的回调逻辑. 广播这种模型中涉及到两个角色,就是广播的发送者和接收者,所以会涉及 ...

  3. “榕树下·那年”移动app ( hybrid ) 开发总结

        榕树下网站本身的技术人员并不多,所以app开发的任务就到了母公司盛大文学这边.       盛大文学无线业务中心负责这次具体开发任务.       一如既往的,开发的情况是:时间紧,任务重,人 ...

  4. CSS 特殊性、继承与层叠

    一.特殊性规则 选择器的特殊性由选择器本身的组件确定:特殊性由四个部分组成,其初始值为0,0,0,0. 1.    对于选择器中的每一个id,记0,1,0,0: 2.    对于选择器中的每一个类.伪 ...

  5. plsql链接数据库配置

    一. 目录结构 D:\install\PLSQL        |-- instantclient_11_2            |-- tnsnames.ora        |-- PLSQL ...

  6. Using KernelShark to analyze the real-time scheduler【转】

    转自:https://lwn.net/Articles/425583/ This article brought to you by LWN subscribers Subscribers to LW ...

  7. C/C++杂记:NULL与0的区别、nullptr的来历

    某些时候,我们需要将指针赋值为空指针,以防止野指针.   有人喜欢使用NULL作为空指针常量使用,例如:int* p = NULL;. 也有人直接使用0值作为空指针常量,例如:int* p = 0;. ...

  8. python网络编程--线程锁(互斥锁Mutex)

    一:为什么需要线程锁 一个进程下可以启动多个线程,多个线程共享父进程的内存空间,也就意味着每个线程可以访问同一份数据,此时,如果2个线程同时要修改同一份数据,会出现什么状况? 很简单,假设你有A,B两 ...

  9. 移动web特殊样式处理

    一.高清图片 二.一像素边框 还有一种解决办法: border-1px($color) position: relative &:after display: block position: ...

  10. WebAssembly 介绍

    http://blog.csdn.net/zhangzq86/article/details/61195685 WebAssembly 的出现是不是意味着 Javascript 要完? https:/ ...