POJ3111 K Best —— 01分数规划 二分法

题目链接:http://poj.org/problem?id=3111

K Best

Time Limit: 8000MS   Memory Limit: 65536K
Total Submissions: 11380   Accepted: 2935
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 = {i1i2, …, 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

题解:

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 #define ms(a,b) memset((a),(b),sizeof((a)))
13 using namespace std;
14 typedef long long LL;
15 const double EPS = 1e-8;
16 const int INF = 2e9;
17 const LL LNF = 2e18;
18 const int MAXN = 1e5+10;
19
20 struct node
21 {
22     double d;
23     int a, b, id;
24     bool operator<(const node x)const{
25         return d>x.d;
26     }
27 }q[MAXN];
28
29 int n, k;
30
31 bool test(double L)
32 {
33     for(int i = 1; i<=n; i++)
34         q[i].d = 1.0*q[i].a - L*q[i].b;
35
36     sort(q+1, q+1+n);
37     double sum = 0;
38     for(int i = 1; i<=k; i++)
39         sum += q[i].d;
40     return sum>0;
41 }
42
43 int main()
44 {
45     while(scanf("%d%d", &n, &k)!=EOF)
46     {
47         for(int i = 1; i<=n; i++)
48         {
49             scanf("%d%d", &q[i].a, &q[i].b);
50             q[i].id = i;
51         }
52
53         double l = 0, r = 1e7;
54         while(l+EPS<=r)
55         {
56             double mid = (l+r)/2;
57             if(test(mid))
58                 l = mid + EPS;
59             else
60                 r = mid - EPS;
61         }
62
63         for(int i = 1; i<=k; i++)
64             printf("%d ", q[i].id);
65         printf("\n");
66     }
67 }

时间: 2024-10-06 00:16:39

POJ3111 K Best —— 01分数规划 二分法的相关文章

[POJ3111]K Best(分数规划, 二分)

题目链接:http://poj.org/problem?id=3111 求选k对数,使得上述式子值最大.容易想到设左边为一个值,对式子变形以下,得到sigma(v-r*w))==0的时候就是最大的,<0是最小的.二分这个r就行了. 1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits>

POJ - 3111 K Best 平均值最大(01分数规划)

题目大意:有n颗珍珠,每颗珍珠有相应的价值和代价,要从中挑出K颗珍珠,使得(这k颗珍珠的总价值/这k颗珍珠的代价 )达到最大 解题思路:平均值最大,要注意精度 给出01分数规划的详细解读 < http://www.cnblogs.com/perseawe/archive/2012/05/03/01fsgh.html> #include<cstdio> #include<algorithm> #include<cstring> using namespace

01分数规划(转)

01分数规划 分类: DP&&记忆化搜索2013-05-04 14:47 4193人阅读 评论(1) 收藏 举报 [关键字] 0/1分数规划.最优比率生成树.最优比率环 [背景] 根据楼教主的回忆录,他曾经在某一场比赛中秒掉了一道最优比率生成树问题,导致很多人跟风失败,最终悲剧.可见最优比率生成树是多么凶残的东西,但是这个东西只要好好研究半天就可以掌握,相信你在看了我写的这篇总结之后可以像楼教主一般秒掉这类问题. 因为网上对于01分数规划问题的详细资料并不是太多,所以我就结合自己的一些理解

专题:01分数规划

poj2976 普通的01分数规划 大意:给定A数组B数组,从中选择N-K个使得R最大,输出Round(100*R); #include <iostream> #include <algorithm> #include <cmath> #include <cstdio> using namespace std; const int N = 1009; const double Eps = 1e-7; int n, k; double a[N], b[N];

【转】[Algorithm]01分数规划

因为搜索关于CFRound277.5E题的题解时发现了这篇文章,很多地方都有值得借鉴的东西,因此转了过来 原文:http://www.cnblogs.com/perseawe/archive/2012/05/03/01fsgh.html [关键字] 0/1分数规划.最优比率生成树.最优比率环 [背景] 根据楼教主的回忆录,他曾经在某一场比赛中秒掉了一道最优比率生成树问题,导致很多人跟风失败,最终悲剧. 自己总结了一些这种问题的解法,因为水平有限,如果有错误或是麻烦的地方,尽管喷,邮箱或是下方留言

Desert King(01分数规划问题)(最优斜率生成树)

Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions:33847   Accepted: 9208 Description David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his count

编程之美第一篇 01分数规划

01分数规划 01分数规划问题其实就是解决单价之类的问题,假设给你n个物品,让你找出选k个物品的最大单价:例如南阳oj:Yougth的最大化:解决这类问题可以用二分查找,这类问题跟二分极大化最小值,极小化最大值有一些相似的地方,均是从结果出发,来进行二分查找:例如上面南阳那道题,可以转化一下: 由于v/w=单价:所以v=w*单价:即v-w*单价=0:有了这个关系,我们马上可以想到二分来查找这个值: 那么我们可以定义一个count数组来记录v-w*单价的值:由于选k个只需要把count从大到小排下

zoj2676--Network Wars(0-1分数规划+最小割)

zoj2676:题目链接 题目大意:有一个n个点的网络,其中有m条光缆(所有的点都被连接,任意两个点之间最多有一条,不存在连接自身的),每条光缆有一定的价值,网络中1为起点,n为终点,现在要求找出一些光缆能分割开1到n,使它们不能相互通信,并且要求花费的和除以光缆数的值最小.输出选择的光缆的编号. 从问题中可以看出一定是0-1分数规划的题目,假设选出光缆的集合M,M为原图的一个割,光缆si∈M,价值为ci,数量k = 1 ,可以推出g(x) = min( ∑c - x*∑k ),因为si是割中的

[POJ 2728]Desert King(0-1分数规划/最优比率生成树)

Description David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his country to bring water to every village. Villages which are connected to his capital village will be