题目链接: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
题解:
http://www.cnblogs.com/DOLFAMINGO/p/7563213.html
代码一:

 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 int n, k;
29
30 bool test(double L)
31 {
32     for(int i = 1; i<=n; i++)
33         q[i].d = 1.0*q[i].a - L*q[i].b;
34
35     sort(q+1, q+1+n);
36     double sum = 0;
37     for(int i = 1; i<=k; i++)   //取前k大的数
38         sum += q[i].d;
39     return sum>=0;
40 }
41
42 int main()
43 {
44     while(scanf("%d%d", &n, &k)!=EOF)
45     {
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 }

View Code

代码二:

 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 id;
24     bool operator<(const node x)const{
25         return d>x.d;
26     }
27 }q[MAXN];
28
29 int n, k;
30 int a[MAXN], b[MAXN];
31
32 bool test(double L)
33 {
34     for(int i = 1; i<=n; i++)   //每一次q[i]都重新更新,与a[i],b[i]独立开来
35     {
36         q[i].id = i;
37         q[i].d = 1.0*a[i] - L*b[i];
38     }
39     sort(q+1, q+1+n);
40     double sum = 0;
41     for(int i = 1; i<=k; i++)
42         sum += q[i].d;
43     return sum>=0;
44 }
45
46 int main()
47 {
48     while(scanf("%d%d", &n, &k)!=EOF)
49     {
50         for(int i = 1; i<=n; i++)
51             scanf("%d%d", &a[i], &b[i]);
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 }

View Code

错误代码:

 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 id;
24     bool operator<(const node x)const{
25         return d>x.d;
26     }
27 }q[MAXN];
28
29 int n, k;
30 int a[MAXN], b[MAXN], ans[MAXN];
31
32 bool test(double L)
33 {
34     //经过一次排序后,q[i].id不再等于i,所以出错。应该同时更新q[i].id
35     for(int i = 1; i<=n; i++)
36         q[i].d = 1.0*a[i] - L*b[i];
37
38     sort(q+1, q+1+n);
39     double sum = 0;
40     for(int i = 1; i<=k; i++)
41         sum += q[i].d;
42     return sum>0;
43 }
44
45 int main()
46 {
47     while(scanf("%d%d", &n, &k)!=EOF)
48     {
49         for(int i = 1; i<=n; i++)
50         {
51             scanf("%d%d", &a[i], &b[i]);
52             q[i].id = i;
53         }
54
55         double l = 0, r = 1e7;
56         while(l+EPS<=r)
57         {
58             double mid = (l+r)/2;
59             if(test(mid))
60                 l = mid + EPS;
61             else
62                 r = mid - EPS;
63         }
64
65         for(int i = 1; i<=k; i++)
66             printf("%d ", q[i].id);
67         printf("\n");
68     }
69 }

View Code

转载于:https://www.cnblogs.com/DOLFAMINGO/p/7571434.html

POJ3111 K Best —— 01分数规划 二分法相关推荐

  1. Bailian4145 放弃考试 POJ2976 ZOJ3068 Dropping tests【二分法+01分数规划】

    4145:放弃考试 总时间限制: 1000ms 内存限制: 65536kB 描述 在一门课程中,一共有n场考试.假如你在i场考试中可以答对bi道题中的ai道,那么你的累计平均分定义为:100·Σai/ ...

  2. poj2976(0-1分数规划)

    0-1分数规划 设x[i]等于1或0. 则我们所求的比率 rate = ∑(cost[i] * x[i]) / ∑(cost[i] * x[i]), 0≤i<m . z( rate ) = ∑( ...

  3. 二分+01分数规划+最大化平均值 Dropping tests POJ - 2976

    题意: 给你若n个分数,分子a[i]a[i]a[i],分母b[i]b[i]b[i],使满足公式100⋅∑i=1nai∑i=1nbi100\cdot\tfrac{\sum_{i=1}^{n} a_{i} ...

  4. 转载二分 01 分数规划即最大化平均值的证明0/1分数规划、最优比率生成树、最优比率环

    首页 新随笔 联系 管理 订阅 随笔- 20  文章- 0  评论- 9 [Algorithm]01分数规划--Update:2012年7月27日 [关键字] 0/1分数规划.最优比率生成树.最优比率 ...

  5. 【转】0-1分数规划等问题

    [关键字] 0/1分数规划.最优比率生成树.最优比率环 [背景] 根据楼教主的回忆录,他曾经在某一场比赛中秒掉了一道最优比率生成树问题,导致很多人跟风失败,最终悲剧.可见最优比率生成树是多么凶残的东西 ...

  6. [Algorithm]01分数规划

    [关键字] 0/1分数规划.最优比率生成树.最优比率环 [背景] 根据楼教主的回忆录,他曾经在某一场比赛中秒掉了一道最优比率生成树问题,导致很多人跟风失败,最终悲剧.可见最优比率生成树是多么凶残的东西 ...

  7. poj2976(0-1 分数规划)

    poj2976(0-1 分数规划) 题意 给定n个二元组 ( a i , b i ) (a_{i},b_{i}) (ai​,bi​)除去n个,剩下的n-k组成集合S作 ∑ i ∈ S a i ∑ i ...

  8. POJ2728 Desert King ——01分数规划Dinkelbach迭代法+最小生成树prim算法

    首先,纪念我用Linux系统AC的第一题-   安装这个万恶的NOI Linux系统费了6小时的时间,不过好在最后终于装上了,但是因为我安装的Linux系统比较烂,还遭到了小花儿和js的鄙视,唉,本人 ...

  9. POJ 2728 01分数规划

    题意: 最优比率生成树,要求生成树中的所有边的花费与所有边的长度的比值最小 题解: 01分数规划,详见http://www.cnblogs.com/proverbs/archive/2013/01/0 ...

最新文章

  1. App列表之圆角ListView源码
  2. centos7中使用yum安装tomcat mysql 等
  3. python 常量_python学习丨变量与常量
  4. 三维重建学习(1):基础知识:旋转矩阵与旋转向量
  5. 无责任畅想:云原生中间件的下一站
  6. vnpy官网说明文档网址
  7. python真假命题_python中的命题演算
  8. Behavior Language Processing with Graph based Feature Generation for Fraud DetectioninOnline Lending
  9. 真正无人驾驶有望在美国全境普及?美交通部准备修改安全法规
  10. mysql关闭显示无权限_如何关闭mysql远程登录权限
  11. visio2016专业版2018最新密钥和下载方法 整理
  12. Mybatis 中事务提交方式
  13. 怎样开始买基金---我的基金购买体验[转]
  14. 解锁三星bl锁有几种方法_三星S8有几种解锁方式?三星S8解锁方法介绍
  15. 如何查看电子元器件的丝印信息
  16. H3C云桌面解决方案介绍
  17. Android 微信h5支付
  18. java mysql物联网土壤智能监控web前端+java后台+数据接程序
  19. 写高效的java代码
  20. IntelliJ IDEA 2018.2.2 x64安装破解教程

热门文章

  1. JSP学习02-config内置对象
  2. 点分十进制IP转换为十进制数以及逆变换,JavaScript
  3. Linux下网络编程
  4. 吃火锅有四忌[转载]
  5. 如何将一键还原精灵备份文件复制出来?
  6. LAMP服务搭建详解
  7. python学习第一周(1)
  8. 设计模式之建造者模式(01)
  9. 沭阳县依托运用大数据推进平安建设
  10. 深入解析Javascript中this关键字的使用