POJ - 3111 K Best 0-1分数规划 二分
| 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
#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分数规划 二分的更多相关文章
- POJ 3111 K Best(01分数规划)
K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 9876 Accepted: 2535 Case Time ...
- POJ - 2976 Dropping tests && 0/1 分数规划
POJ - 2976 Dropping tests 你有 \(n\) 次考试成绩, 定义考试平均成绩为 \[\frac{\sum_{i = 1}^{n} a_{i}}{\sum_{i = 1}^{n} ...
- poj 2976 Dropping tests 0/1分数规划
0/1分数规划问题,用二分解决!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> # ...
- POJ 2976 Dropping tests 【01分数规划+二分】
题目链接:http://poj.org/problem?id=2976 Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total S ...
- LOJ149 0/1分数规划
竟然没有写过分数规划的题解 考前挣扎一发板子( 二分答案k 然后0/1分数规划的方法就是 分母乘过去然后贪心解决 注意实数二分的精度 一般估计一个次数比较好不然容易出现精度比较误差[惨痛教训 就做完了 ...
- POJ 2976 Dropping tests【0/1分数规划模板】
传送门:http://poj.org/problem?id=2976 题意:给出组和,去掉对数据,使得的总和除以的总和最大. 思路:0/1分数规划 设,则(其中等于0或1) 开始假设使得上式成立,将从 ...
- POJ2976 题解 0/1分数规划入门题 二分
题目链接:http://poj.org/problem?id=2976 关于 0/1分数规划 参见 这篇博客 实现代码如下: #include <cstdio> #include < ...
- poj 3111 K Best 最大化平均值 二分思想
poj 3111 K Best 最大化平均值 二分思想 题目链接: http://poj.org/problem?id=3111 思路: 挑战程序竞赛书上讲的很好,下面的解释也基本来源于此书 设定条件 ...
- LOJ 3089 「BJOI2019」奥术神杖——AC自动机DP+0/1分数规划
题目:https://loj.ac/problem/3089 没想到把根号之类的求对数变成算数平均值.写了个只能得15分的暴力. #include<cstdio> #include< ...
- Bzoj1486/洛谷P3199 最小圈(0/1分数规划+spfa)/(动态规划+结论)
题面 Bzoj 洛谷 题解(0/1分数规划+spfa) 考虑\(0/1\)分数规划,设当前枚举到的答案为\(ans\) 则我们要使(其中\(\forall b_i=1\)) \[ \frac{\sum ...
随机推荐
- angularJS 控制输入的百分数在0%-100%之间
想了老半天了,记录一下 app.directive("percentageCheck", function () { return { restrict: 'A', require ...
- linux学习记录.3.virtualbox 共享文件夹
需要先安装增强功能. 设置目录后, mkdir /mnt/WinDownload //建立映射目录 sudo mount -t vboxsf Download /mnt/Windwnload ...
- C# FileStream MemoryStream BufferedStream StreamReader StreamWriter
FileStream读取文件 , array.Length);//读取流中数据把它写到字节数组中file.Close();//关闭流string str =Encoding.Default.GetSt ...
- 简析CSRF
1.简介 CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写为:CSRF/XSRF ...
- 配置虚拟机时间使其与国内时间同步,linux时间 ntp
设置系统时间 [root@node2 ~]# date -s "10/30/18 09:30:00"Tue Oct 30 09:30:00 PDT 2018[root@node2 ...
- [Ubuntu 14.04] 安装Flash && 安装QQ
一.安装Flash 打开Firefox浏览器弹出的Flash安装提醒早都烦死了,那么Ubuntu14.04怎么安装Flash呢? 1.32位系统命令行安装: 第一步 更新库: sudo apt-get ...
- jmeter,测登录,要不要过滤掉JS,CSS等请求?感觉过滤掉了压出来的数据就不真实?
首先,我们来明确下你的性能测试目的,你的目的是服务端的性能还是前端的性能.这两用目的所涉及到的测试场景和工具等方法是不一样的.1.我们先来谈谈服务端的性能.一般的web产品,像css, jpeg等这种 ...
- asp.net 伪静态实现(UrlRewritingNet)
UrlRewritingNet.UrlRewriter源码地址 https://github.com/aspnetde/UrlRewritingNet部署步骤: 步骤一: <!--只允许存在一个 ...
- Tomcat实现多域名之间session共享
最近启用二级域名后,面临一个主域名与二级域名之间 session 不能共享的问题,带来的麻烦就是用户在主域名登陆,但由于二级域名 session 不能共享 ,因此无法进行登陆的操作,对一些功能有一些影 ...
- shell 数组基础->
数组其实也算是变量, 传统的变量只能存储一个值, 但数组可以存储多个值. 普通数组:只能使用整数 作为数组索引 [有序 0 1 2 3 4 ]关联数组:可以使用字符串 作为数组索引 [无序 name ...