POJ3111 K Best —— 01分数规划 二分法
题目链接:http://poj.org/problem?id=3111
| 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 = {i1, i2, …, 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
#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分数规划 二分法的更多相关文章
- POJ - 3111 K Best 0-1分数规划 二分
K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 12812 Accepted: 3290 Case Time ...
- [POJ3111]K Best(分数规划, 二分)
题目链接:http://poj.org/problem?id=3111 求选k对数,使得上述式子值最大.容易想到设左边为一个值,对式子变形以下,得到sigma(v-r*w))==0的时候就是最大的,& ...
- POJ2976 Dropping tests —— 01分数规划 二分法
题目链接:http://poj.org/problem?id=2976 Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total S ...
- 01分数规划问题(二分法与Dinkelbach算法)
链接 前置技能 二分思想 最短路算法 一些数学脑细胞? 问题模型1基本01分数规划问题给定n个二元组(valuei,costi),valuei是选择此二元组获得的价值(非负),costi是选择此二元组 ...
- 【转】[Algorithm]01分数规划
因为搜索关于CFRound277.5E题的题解时发现了这篇文章,很多地方都有值得借鉴的东西,因此转了过来 原文:http://www.cnblogs.com/perseawe/archive/2012 ...
- POJ 2976 Dropping tests 01分数规划 模板
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6373 Accepted: 2198 ...
- Desert King(01分数规划问题)(最优斜率生成树)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions:33847 Accepted: 9208 Descr ...
- 【poj 2976】Dropping tests(算法效率--01分数规划 模版题+二分){附【转】01分数规划问题}
P.S.又是一个抽时间学了2个小时的新东西......讲解在上半部分,题解在下半部分. 先说一下转的原文:http://www.cnblogs.com/perseawe/archive/2012/05 ...
- POJ3621Sightseeing Cows[01分数规划 spfa(dfs)负环 ]
Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9703 Accepted: 3299 ...
随机推荐
- charts 画折线图
主题:指定日期内,不同地区的发布信息的数量的变化曲线 数据库是mongod 数据是58同城的发布的信息 整体思路: 1由于从数据库中拿到的数据,格式等方面并不一样能完全满足需求,需要对数据库中的数据进 ...
- linux虚拟机无法上网 Network is unreachable
系统centos 安装ftp时报错 Couldn't resolve host 'mirrorlist.centos.org [root@wulihua bin]# yum install vsft ...
- uva 11916 解模方程a^x=b (mod n)
Emoogle Grid You have to color an M x N ( 1M, N108) two dimensional grid. You will be provided K ...
- BZOJ1902: Zju2116 Christopher
$n \leq 10^{100}$,问$C_n^m,0<=m<=n$有多少是质数$p \leq 1e7$的倍数. 一样,套高精度的题,只有战胜他才能鄙视他. 但是我TM被他鄙视了一上午!! ...
- ubuntu下安装翻译软件
原文: http://sixipiaoyang.blog.163.com/blog/static/6232358820144146386437/ Ubuntu下常用的翻译软件有StarDict,Gol ...
- 计算机操作系统处理机调度读后感—–关于进程概念的剖析。从RING3到RING0(32位操作系统)
计算机操作系统处理机调度读后感: 笔者在看操作系统西安电子科技大学那本书的时候,初次感觉本科教的不会太难,所以没有认真的看,但是随后这本书讲的刷新了我的世界观.这本书居然是ring0级别的,这时不禁吐 ...
- Raft算法详解
一致性算法Raft详解 背景 熟悉或了解分布性系统的开发者都知道一致性算法的重要性,Paxos一致性算法从90年提出到现在已经有二十几年了,而Paxos流程太过于繁杂实现起来也比较复杂,可能也是以为过 ...
- fatal error C1189: #error : core.hpp header must be compiled as C++
两次opencv工程需要设置为C++编译:找了一半天的解决方法. I am building a C application that uses OpenCV. when compiling, I g ...
- 【LeetCode】Generate Parentheses 解题报告
[题目] Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...
- Variable 'bop' is uninitialized when captured by block
代码: - (void)doTest { NSBlockOperation * bop = [NSBlockOperation blockOperationWithBlock:^{ if (!bop. ...