题目链接: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. wsgi 简介

    原文博客地址 http://blog.csdn.net/on_1y/article/details/18803563

  2. uva 10140 素数筛选(两次)

    #include<iostream> #include<cstring> #include<cmath> #include<cstdio> using ...

  3. 【索引】理解MySQL——索引与优化

    MySQL 索引 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索 ...

  4. python中单引号,双引号,三引号的比较 转载

    本文转载自http://blog.sina.com.cn/s/blog_6be8928401017lwy.html 先说1双引号与3个双引号的区别,双引号所表示的字 符串通常要写成一行 如: s1 = ...

  5. android的系统学习

    先从Android的应用开发开始,等到对应用掌握的比较熟悉了,开始慢慢阅读一些Android 应用框架层的源代码,然后再渐渐往下去了解Android的JNI.Libraries.Dalvik虚拟机.H ...

  6. POJ2752 NEXT[J]特性应用利用。

    题意:求一个字符串所有的前缀和后缀相同的情况,每个情况输出长度,如 ababcababababcabab :2 4 9 18 思路:next数组应用,利用j=nxet[i],i之前与开头相同的字符串长 ...

  7. Ajax 实现文件的下载

    JQuery的ajax函数的返回类型只有xml.text.json.html等类型,没有“流”类型,所以我们要实现ajax下载,不能够使用相应的ajax函数进行文件下载.但可以用js生成一个form, ...

  8. HDU 5667 Sequence【矩阵快速幂+费马小定理】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5667 题意: Lcomyn 是个很厉害的选手,除了喜欢写17kb+的代码题,偶尔还会写数学题.他找到 ...

  9. Keil建立第一个C51工程的步骤

    参见51+arm开发板<使用手册.pdf> 1.“project”   >>  “new project”  >>  新建一个用于保存工程的文件夹例如dem  &g ...

  10. CODEVS 1245 最小的N个和 堆+排序

    原题链接 http://codevs.cn/problem/1245/ 题目描述 Description 有两个长度为 N 的序列 A 和 B,在 A 和 B 中各任取一个数可以得到 N^2 个和,求 ...