题目链接: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. 路飞学城详细步骤 part1

    详细步骤 1 添加登录页面 步骤: Header.vue 写一个登录按钮,<router-link to = ' /xx'> 在路由的 index.js中添加这个 新的路由,{'path' ...

  2. 自己写的java返回结果集封装

    import java.io.Serializable; import com.fasterxml.jackson.core.JsonProcessingException; import com.f ...

  3. 【HDOJ5952】Counting Cliques(团,dfs)

    题意:给定一张n点m边的图,求大小为S的团的个数 N ≤ 100,M ≤ 1000,2 ≤ S ≤ 10,保证点的度不超过20 思路:dfs 因为每个点可能不止属于一个极大团,所以不能求出极大团然后计 ...

  4. Laravel5.1 报错:控制器不存在

    Laravel5.1 报错:控制器不存在 错误提示: Class App\Http\Controllers\Api/UserController does not exist 解决: (1)检查控制器 ...

  5. vue之监听事件

    一.v-on 可以用 v-on 指令监听 DOM 事件,并在触发时运行一些 JavaScript 代码. 简写形式  用@代替 v-on: <button v-on:click="co ...

  6. Java 5/Java 6/Java7/Java 8新特性收集

    前言: Java 8对应的JDK版本为JDK8,而官网下载回来安装的时候,文件夹上写的是JDK1.8,同一个意思.(而这个版本命名也是有规律的,以此类推) 一.Java 5 1.https://seg ...

  7. 【swagger】1.swagger提供开发者文档--简单集成到spring boot中【spring mvc】【spring boot】

    swagger提供开发者文档 ======================================================== 作用:想使用swagger的同学,一定是想用它来做前后台 ...

  8. mysql 建立utf8字符集数据库

    CREATE DATABASE `evaluate` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

  9. pandas入门指南

    上一篇讲了numpy,除此之外,还有一个工具我们一定会使用,那就是pandas.如果说numpy中数据存储形式是列表的话,那么pandas中数据的存储形式更像是字典.为什么这么说呢?因为pandas中 ...

  10. UI标签库专题二:JEECG智能开发平台Column(列) 子标签

    UI标签库专题二:JEECG智能开发平台Column(列) 子标签  1.1. Column(列) 子标签 1.1.1. 演示样例 <t:dgCol title="年龄" ...