题目传送门

题意就是,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

poj3111 选取物品(二分+贪心)的更多相关文章

  1. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  2. 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心

    /** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...

  3. 【bzoj2097】[Usaco2010 Dec]Exercise 奶牛健美操 二分+贪心

    题目描述 Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径. ...

  4. Codeforces_732D_(二分贪心)

    D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  5. CF732D Exams 二分 贪心

    思路:二分+贪心 提交次数:10次以上 错因:刚开始以为二分(边界,$+1or-1$)写错了,调了半天,后来才发现是$ck()$写错了.开始只判了最后是否小于零,而应该中间一旦小于零就$return\ ...

  6. $CF949D\ Curfew$ 二分/贪心

    正解:二分/贪心 解题报告: 传送门$QwQ$ 首先这里是二分还是蛮显然的?考虑二分那个最大值,然后先保证一个老师是合法的再看另一个老师那里是否合法就成$QwQ$. 发现不太会搞这个合不合法的所以咕了 ...

  7. $bzoj2067\ szn$ 二分+贪心

    正解:二分+贪心 解题报告: 传送门$QwQ$ 题目大意就说有一棵树,然后要用若干条线覆盖所有边且不能重叠.问最少要用几条线,在用线最少的前提下最长的线最短是多长. 昂首先最少用多少条线这个还是蛮$e ...

  8. leetcode1552题解【二分+贪心】

    leetcode1552.两球之间的磁力 题目链接 算法 二分+贪心 时间复杂度O(nlogn + nlogm) 1.根据题意描述,我们需要将m个球放入到n个篮子中,根据题目中数据范围描述发现m &l ...

  9. 【二分 贪心】bzoj3477: [Usaco2014 Mar]Sabotage

    科学二分姿势 Description Farmer John's arch-nemesis, Farmer Paul, has decided to sabotage Farmer John's mi ...

随机推荐

  1. Solr5.5.3的研究之路 ---1、从Mysql导入数据并创建索引

    公司需要用到全文检索,故使用Solr,也是新人一枚,本人查看的前提是Solr已经安装部署成功,我用的服务器是自带的Jetty 1.创建Collection [root@whoami bin]# ./s ...

  2. jstl 判断 null

    <c:if test="${not empty object }"> ${object}不为空 </c:if>

  3. java之类的封装

    类和对象成员变量,成员函数特殊的成员变量和成员函数 函数重载 构造函数 静态变量 静态函数面向对象:封装 继承 多态封装 Encapsulation为什么需要封装?外界无法直接操作对象的具体的属性(安 ...

  4. PHP中几种加密形式

    1.Md5加密和Crypt都是单向加密: 登陆时把登录密码转为md5值,然后和数据库中的进行比较. 其中crypt中的盐值支持多种: 以CRYPT_STD_DES是以/0-9A-Za-z/中的两个字符 ...

  5. oracle语法练习汇总

    全是自己一个一个敲出来的啊 啊 啊 --(1)查询20号部门的所有员工信息. --(2)查询所有工种为CLERK的员工的工号.员工名和部门名. select e.empno,e.ename,d.dna ...

  6. 2018网络预选赛 青岛 H

    题目链接:https://pintia.cn/problem-sets/1036903825309761536/problems/1041156323504345088 题意:小明从某一点出发,向右方 ...

  7. SWT简介

    --------------siwuxie095                         SWT 简介:     SWT(Standard Widget Toolkit) 也是基于一个对等体实 ...

  8. (转)C++中使用C代码

    昨晚看书的时候碰到一个问题,在C++中如何调用C代码...于是查了一下资料...发现了一个大神写的文章挺好的. -------------------------------------------- ...

  9. Luogu 3934 Nephren Ruq Insania

    和Ynoi2016 炸脖龙重题了. BZOJ 5394. 首先是扩展欧拉定理: 一开始傻掉了……递归的层数和区间长度无关……也就是说我们每一次直接暴力递归求解子问题一定不会超过$logP$层,因为当模 ...

  10. Django框架 之 Cookie和Session初识

    Django框架 之 Cookie和Session初识 浏览目录 Cookie介绍 Django中的Cookie Session 一.Cookie介绍 1.Cookie产生的意义 众所周知,HTTP协 ...