POJ 3111
| Time Limit: 8000MS | Memory Limit: 65536K | |
| Total Submissions: 5177 | Accepted: 1411 | |
| 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 <algorithm>
#include <utility> using namespace std; #define maxn 100005
#define eps 1e-8
typedef pair<double,int> pii; int w[maxn],v[maxn];
double f[maxn];
pii ans[maxn];
int n,k; bool judge(double x) {
for(int i = ; i <= n; ++i) {
f[i] = (v[i] - x * w[i]);
}
sort(f + ,f + n + ); double sum = ;
for(int i = n; i >= n - k + ; --i) {
sum += f[i];
} return sum >= ; } void output(double x) { for(int i = ; i <= n; ++i) {
ans[i] = make_pair(v[i] - x * w[i],i);
}
sort(ans + ,ans + n + ); printf("%d",ans[n].second);
for(int i = n - ; i >= n - k + ; --i) {
printf(" %d",ans[i].second);
} printf("\n"); } void solve() {
double l = ,r = ;
for(int i = ; i <= n; ++i) {
r += v[i];
} int num = ;
while(num++ < ) {
//printf(" l = %f r = %f\n",l,r);
//num++;
double mid = (l + r) / ;
if(judge(mid)) {
l = mid;
} else {
r = mid;
} } //printf("%f\n",l);
output(l);
}
int main()
{ //freopen("sw.in","r",stdin); while(~scanf("%d%d",&n,&k))
{
for(int i = ; i <= n; ++i) {
scanf("%d%d",&v[i],&w[i]);
} solve();
} return ;
}
POJ 3111的更多相关文章
- poj 3111 K Best 最大化平均值 二分思想
poj 3111 K Best 最大化平均值 二分思想 题目链接: http://poj.org/problem?id=3111 思路: 挑战程序竞赛书上讲的很好,下面的解释也基本来源于此书 设定条件 ...
- POJ 3111 K Best(01分数规划)
K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 9876 Accepted: 2535 Case Time ...
- POJ 3111 K Best(二分答案)
[题目链接] http://poj.org/problem?id=3111 [题目大意] 选取k个物品,最大化sum(ai)/sum(bi) [题解] 如果答案是x,那么有sigma(a)>=s ...
- Divide and conquer:K Best(POJ 3111)
挑选最美的珠宝 题目大意:挑选k个珠宝使得∑a/∑b最大,输出组合数 最大化平均值的标准题型,二分法就好了,一定要注意范围(10e-7),如果是10e-8就会tle,10e-6就是wa #inclu ...
- poj 3111 K Best (二分搜索之最大化平均值之01分数规划)
Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...
- POJ 3111 K Best(最大化平均值)
题目链接:click here~~ [题目大意]有n个物品的重量和价值各自是Wi和Vi.从中选出K个物品使得单位重量的价值最大,输出物品的编号 [解题思路]:最大化平均值的经典.參见click her ...
- POJ 3111 K Best
二分,排序,贪心. 最优比率生成树,可以二分$+$贪心来实现,不过这样做精度不行. 如果是这样一个问题,该如何解决:问你$n$个里面选择$k$个,能否使得$\frac{{\sum\limits_{j ...
- K Besk [POJ 3111]
描述 Demy有n颗宝石.她的每个珠宝都有一些价值vi和重量wi.自从丈夫约翰在最近的金融危机爆发后,已经决定出售一些珠宝.她决定自己会保留最好的珠宝.她决定保留这样的宝石,使他们的具体价值尽可能大. ...
- POJ - 3111 K Best 0-1分数规划 二分
K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 12812 Accepted: 3290 Case Time ...
随机推荐
- THREE.js代码备份——canvas_lines(随机点、画线)
<!DOCTYPE html> <html lang="en"> <head> <title>three.js canvas - l ...
- 解决Eclipse中Java工程间循环引用而报错的问题
如果myeclipse 报如下错误 A cycle was detected in the build path of project 如果我们的项目包含多个工程(project),而它们之间又是循 ...
- iOS开发笔记-两种单例模式的写法
iOS开发笔记-两种单例模式的写法 单例模式是开发中最常用的写法之一,iOS的单例模式有两种官方写法,如下: 不使用GCD #import "ServiceManager.h" ...
- 《Apache负载均衡》RHEL6
Apache负载均衡的搭建,基于上篇<CDN web加速代理>搭建好的服务器,我们来搭建apache负载均衡: Apahe负载均衡:就是为了缓解一台服务器的压力而多台服务器配合使用. 基于 ...
- javascript中的光标
最近项目中要做一个键盘操作,光标移动的功能:增强用户体验:问朋友查资料了解到这方面的知识:整理备忘: 1.IE使用textRange对象,其他使用selectionStart selectionEnd ...
- userscript.user.js 文件头
// ==UserScript== // @name MyUserScript // @namespace https://github.com/MrLeo // @description users ...
- 单元测试SimpleTest新手入门
最近学习单元测试,先用了下PHPunit,结果安装问题一大堆,于是立刻放弃改试simpletest,感觉简单多了.下面列出步骤. 1.下载simpletest(版本1.1.0), http://www ...
- phpcms v9 源码解析(4)content模块下的index.php文件的init()方法解析
在了解index.php中的init函数的时候,让我们先看看最开始的几行代码 1-5 第二行, defined('IN_PHPCMS') or exit('Nopermission resource ...
- 如何在C#中实现图片缩放
//下面给出三个简单的方法,后面两个方法是扩展,估计有时用得着 //************************************************************// /// ...
- 第27章 项目8:使用XML-RPC进行文件共享
1.问题 创建一个简单的P2P文件共享程序. P2P文件共享程序是在不同计算机上的程序交换文件.P2P交互内,任何节点(peer)都可以是链接到其他节点.在这样一个由节点组成的虚拟网络中,是没有中央节 ...