题目传送门

题意就是,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. Result Maps、Auto-mapping、cache

    1.  Result Maps resultMap元素是Mybatis里面最重要的并且功能最强大的一个元素.(The resultMapelement is the most important an ...

  2. xftp permission is not allowed

    上传文件时出现此项错误: permission is not allowed, 原因: 1.可能vsfptd没有配置好 写权限没有开启 2.可能是当前的目录权限,上传的用户不具有: 比如:用户hado ...

  3. java线程游戏之背景图片的移动

    package com.plane; import java.awt.Graphics; import java.awt.Image; import javax.swing.ImageIcon; im ...

  4. import configparser

  5. 【总结整理】display与position之间的关系【较完整】(转)

    display与position之间的关系   以防自己忘记写的 网上找的 positon 与 display 的相互关系 元素分为内联元素和区块元素两类(当然也有其它的),在内联元素中有个非常重要的 ...

  6. oracle语法练习汇总

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

  7. var_dump — 打印变量的相关信息

    <?php $a = array( 1 , 2 , array( "a" , "b" , "c" )); var_dump ( $a ...

  8. javascript 函数,事件

    1.函数字符串函数 var s=new string(); var ss="hello world"; var sss=""HELLO, WORLD" ...

  9. c#基础 函数

    函数  就是方法 独立完成某项功能的一个个体 函数的的三个好处:1.提高代码的重用性.2.提高功能开发的效率,3.提高代码的可维护性. 函数也分为     固定功能函数, 高度抽象函数.高度抽象函数应 ...

  10. JS jquery ajax 已看1 有用

    4.form中的input可以设置为readonly和disable,请问2者有什么区别? readonly不可编辑,但可以选择和复制:值可以传递到后台 disabled不能编辑,不能复制,不能选择: ...