题目传送门

题意就是,n个物品,从中选取k个,要按照那个公式所取的值最大。

思路:最大化平均值的时候首先想到的就是二分, 我们设G(x) 为单位的重量不小于X, 我们的目标就是要找到满足条件的最大的X, 也就是说我们的OK函数就要判断是否能够找到一个大小为K的集合S, 使得单位重量的价值大于等于X, 我们将当时变形转化为vi - x*wi >= 0  对于 所有的i 属于集合S, 这时我们对于一个整体值vi-x*wi的值的 排序, 贪心的选取前K个, 这样OK函数就写好了。

但是这道题最恶心的地方是,卡常数,如果要用c++交,你的排序函数就必须 引用,g++交好像就没关系。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<math.h>
#include<cmath>
#include<time.h>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<numeric>
using namespace std;
typedef long long ll;
const int maxn=2000010;
const int INF=0x3f3f3f3f;
struct dian{double v,w;double y;int id;
}a[maxn];
double mps=1e10,eps=1e-9;
double l,r,mid;
int n,k;
bool cmp(const dian& a,const dian& b){return a.y>b.y;
}
bool ok(double d){double e=0;for(int i=1;i<=n;i++){a[i].y=a[i].v-a[i].w*d;}sort(a+1,a+1+n,cmp);for(int i=1;i<=k;i++){e+=a[i].y;}return e>=0;
}
int main(){scanf("%d%d",&n,&k);for(int i=1;i<=n;i++){scanf("%lf%lf",&a[i].v,&a[i].w);a[i].id=i;}l=0,r=1e13;while(r-l>eps){mid=(l+r)/2;if(ok(mid)){l=mid;}else{r=mid;}}for(int i=1;i<=k;i++){printf("%d ",a[i].id);}printf("\n");
}
K Best
Time Limit: 8000MS   Memory Limit: 65536K
Total Submissions: 13263   Accepted: 3388
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

转载于:https://www.cnblogs.com/mountaink/p/9536713.html

poj3111 选取物品(二分+贪心)相关推荐

  1. 蒟蒻的第一篇博客CF1041C Coffee Break(二分+贪心+set)

    CF1041C Coffee Break(二分+贪心+set) 描述 Recently Monocarp got a job. His working day lasts exactly mm min ...

  2. 洛谷P1182 数列分段 Section II(二分+贪心)

    题目描述 对于给定的一个长度为N的正整数数列 A1∼NA _{1∼N}A1∼N​,现要将其分成 M(M≤N)M(M≤N)M(M≤N)段,并要求每段连续,且每段和的最大值最小. 关于最大值最小: 例如一 ...

  3. [USACO16JAN]Angry Cows S[二分+贪心]

    题意:Bessie 设计了一款新游戏:Angry Cows.在这个游戏中,玩家发射奶牛,每头奶牛落地时引爆一定范围内的干草.游戏的目标是使用一组奶牛引爆所有干草. N 捆干草排列在数轴上的不同位置.第 ...

  4. BZOJ2525 [Poi2011]Dynamite 【二分 + 贪心】

    题目链接 BZOJ2525 题解 就是要求所有有炸弹的点到点燃点距离最大值最小 显然二分答案距离\(D\) 然后按深度排序,贪心点燃当前没覆盖的深度最深的点往上第\(D\)层的点 每覆盖一个点要标记其 ...

  5. hdu5248序列变换(二分+贪心)基础题

    题意(中文的直接粘题意吧)                                                                                        ...

  6. 外卖(food) 洛谷4040宅男计划 三分套二分贪心

    food评测传送门 [题目描述] 叫外卖是一个技术活,宅男宅女们一直面对着一个很大的矛盾,如何以有限的金钱在宿舍宅得尽量久.     外卖店一共有 N 种食物,每种食物有固定的价钱 Pi 与保质期 S ...

  7. LA 4254 Processor 处理器 【二分 贪心 优先队列】

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21663 二分: 最大值最小的问题通过二分来求解.对处理器速度进行 ...

  8. hdu 5248(二分+贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5248 解题思路:这道题我原本的思路是动态规划,结果看到数很大,结果放弃了.然后想到二分,思路是对的,只 ...

  9. CodeForces - 551C GukiZ hates Boxes(二分+贪心)

    题目链接:点击查看 题目大意:给出一个长度为 n 的数列 a,表示每个位置的障碍物数量,现在有 m 个学生可以来搬走障碍物,每一秒钟可以做出的行为如下: 从位置 i 移动到位置 i + 1 从当前位置 ...

最新文章

  1. SUN dataset图像数据集下载
  2. Oracle数据库之单行函数
  3. 3种设计模式java小程序_Java设计模式之单例模式(3种实现方式)
  4. 空投坐标怎么看6_装修时,怎么确认自己买的“瓷砖”是优等品?看“6点”很重要...
  5. C语言中可变参数列表
  6. 如何判断两个时间段是否有交集
  7. 闪存必须解决的三大问题
  8. 计算机组装报告目录,计算机组装实验报告(共9篇).doc
  9. MYSQL无法连接,提示10055错误尝试解决
  10. js写用户名密码错误提示_写给前端的 Jenkins 教程——快速实现前端/ Node.js 项目的 CI/CD...
  11. java小游戏制作(maxfo)
  12. SPI协议通信时序详解
  13. Python读取pdf无框线表格_Python将PDF数据解析为表格形式
  14. 分步图解分析排序方法-冒泡排序
  15. Adobe Creative Cloud登陆中国大陆市场
  16. 国外开放知识图谱_什么是开放知识,如何传播?
  17. 帆软报表扩展列计算同比环比
  18. #023单词接龙1(字符串)(女友)
  19. 戴尔服务器r720怎么进入系统安装,戴尔R720服务器安装步骤.doc
  20. python打印等腰三角形_Python 打印各种三角形

热门文章

  1. csapp学习笔记2021.1.9
  2. 移动零—leetcode283
  3. 深入理解BN、合并conv+BN公式推导
  4. 关于IPC入侵的一些细节
  5. redis的关键路径和lazy-free
  6. Python3 注释
  7. 为什么要学Access?
  8. 运行mysql数据库的命令_mysql 数据库常用命令
  9. 提取一个二进制位最右侧的1
  10. NDK建立多个共享库