题目链接: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
 
 
 
 
题解:
 
 
代码一:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e5+; struct node
{
double d;
int a, b, id;
bool operator<(const node x)const{
return d>x.d;
}
}q[MAXN];
int n, k; bool test(double L)
{
for(int i = ; i<=n; i++)
q[i].d = 1.0*q[i].a - L*q[i].b; sort(q+, q++n);
double sum = ;
for(int i = ; i<=k; i++) //取前k大的数
sum += q[i].d;
return sum>=;
} int main()
{
while(scanf("%d%d", &n, &k)!=EOF)
{
//一次性把所有信息都录入结构体中,当排序时,即使打乱了顺序,仍然还记得初始下标。
for(int i = ; i<=n; i++)
{
scanf("%d%d", &q[i].a, &q[i].b);
q[i].id = i;
} double l = , r = 1e7;
while(l+EPS<=r)
{
double mid = (l+r)/;
if(test(mid))
l = mid + EPS;
else
r = mid - EPS;
} for(int i = ; i<=k; i++)
printf("%d ", q[i].id);
printf("\n");
}
}
 
代码二:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e5+; struct node
{
double d;
int id;
bool operator<(const node x)const{
return d>x.d;
}
}q[MAXN]; int n, k;
int a[MAXN], b[MAXN]; bool test(double L)
{
for(int i = ; i<=n; i++) //每一次q[i]都重新更新,与a[i],b[i]独立开来
{
q[i].id = i;
q[i].d = 1.0*a[i] - L*b[i];
}
sort(q+, q++n);
double sum = ;
for(int i = ; i<=k; i++)
sum += q[i].d;
return sum>=;
} int main()
{
while(scanf("%d%d", &n, &k)!=EOF)
{
for(int i = ; i<=n; i++)
scanf("%d%d", &a[i], &b[i]); double l = , r = 1e7;
while(l+EPS<=r)
{
double mid = (l+r)/;
if(test(mid))
l = mid + EPS;
else
r = mid - EPS;
} for(int i = ; i<=k; i++)
printf("%d ", q[i].id);
printf("\n");
}
}

错误代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e5+; struct node
{
double d;
int id;
bool operator<(const node x)const{
return d>x.d;
}
}q[MAXN]; int n, k;
int a[MAXN], b[MAXN], ans[MAXN]; bool test(double L)
{
//经过一次排序后,q[i].id不再等于i,所以出错。应该同时更新q[i].id
for(int i = ; i<=n; i++)
q[i].d = 1.0*a[i] - L*b[i]; sort(q+, q++n);
double sum = ;
for(int i = ; i<=k; i++)
sum += q[i].d;
return sum>;
} int main()
{
while(scanf("%d%d", &n, &k)!=EOF)
{
for(int i = ; i<=n; i++)
{
scanf("%d%d", &a[i], &b[i]);
q[i].id = i;
} double l = , r = 1e7;
while(l+EPS<=r)
{
double mid = (l+r)/;
if(test(mid))
l = mid + EPS;
else
r = mid - EPS;
} for(int i = ; i<=k; i++)
printf("%d ", q[i].id);
printf("\n");
}
}
 
 

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

  1. POJ - 3111 K Best 0-1分数规划 二分

    K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 12812   Accepted: 3290 Case Time ...

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

    题目链接:http://poj.org/problem?id=3111 求选k对数,使得上述式子值最大.容易想到设左边为一个值,对式子变形以下,得到sigma(v-r*w))==0的时候就是最大的,& ...

  3. POJ2976 Dropping tests —— 01分数规划 二分法

    题目链接:http://poj.org/problem?id=2976 Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  4. 01分数规划问题(二分法与Dinkelbach算法)

    链接 前置技能 二分思想 最短路算法 一些数学脑细胞? 问题模型1基本01分数规划问题给定n个二元组(valuei,costi),valuei是选择此二元组获得的价值(非负),costi是选择此二元组 ...

  5. 【转】[Algorithm]01分数规划

    因为搜索关于CFRound277.5E题的题解时发现了这篇文章,很多地方都有值得借鉴的东西,因此转了过来 原文:http://www.cnblogs.com/perseawe/archive/2012 ...

  6. POJ 2976 Dropping tests 01分数规划 模板

    Dropping tests   Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6373   Accepted: 2198 ...

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

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions:33847   Accepted: 9208 Descr ...

  8. 【poj 2976】Dropping tests(算法效率--01分数规划 模版题+二分){附【转】01分数规划问题}

    P.S.又是一个抽时间学了2个小时的新东西......讲解在上半部分,题解在下半部分. 先说一下转的原文:http://www.cnblogs.com/perseawe/archive/2012/05 ...

  9. POJ3621Sightseeing Cows[01分数规划 spfa(dfs)负环 ]

    Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9703   Accepted: 3299 ...

随机推荐

  1. Nk 1430 Divisors(因子数与质因数)

    Time Limit: 5000 ms    Memory Limit: 10000 kB   Total Submit : 432 (78 users)   Accepted Submit : 10 ...

  2. gdbt原理解析

    链接: http://note.youdao.com/noteshare?id=aeb1c7a30c5f4b70e3fff51f28ee5c47 懒得复制到这里了,一开始是在有道云笔记上写的,这里的公 ...

  3. gitweb 搭建教程

    1. 前言 git 是一个版本控制工具,类似svn. 本文内容主要涉及git仓库通过浏览器访问(用web的方式去查看git提交历史记录,tag,branch等信息),即gitweb. 效果图: 在这里 ...

  4. numpy数组之读写文件

    目录 通过 numpy 读写 txt 或 csv 文件 通过 numpy 读写 npy 或 npz 文件 读写 npy 文件 读写 npz 文件 通过 h5py 读写 hdf5 文件 简单读取 通过切 ...

  5. Java对象的死亡

    在堆里面存放着Java世界中几乎所有的对象实例,垃圾收集器在对堆进行回收前,第一件事情就是要确定这些对象之中哪些还“存活”着,哪些已经“死去”(即不可能再被任何途径使用的对象). 一,引用计数算法 给 ...

  6. 空暇时候思考2(&#39;\0&#39;等价于数字0还是字符0)

    /********************************************************************** * * Copyright (c)2015,WK Stu ...

  7. Mqtt协议IOS端移植3

    ServerMqFramework.h #import "MqttFramework.h" @interface ServerMqFramework : MqttFramework ...

  8. Solidedge如何修改特征的参数

    我已经长出了60MM,现在发现不对,要改成50MM.右击这个特征,点击编辑定义   直接左键单击尺寸,修改数据,按回车,鼠标右键,即可.    

  9. vs2015使用Git管理项目

    初级 1,在码云上去注册一个帐号(码云的私有库是免费的,安全性怎么样我不知道) 2,在码云上新建一个项目,把相关的开发人员加到这个项目里,会得到这个项目在码云上的远程仓库的地址. 3,打开vs2015 ...

  10. Eclipse打包Android项目时用到proguard.cfg后,出现的Warning:can&#39;t find referenced class问题的解决方式

    Warning: can't find superclass or interface Warning: can't find referenced class 这两个问题的解决方法: 1.要把你项目 ...