K Best
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 ≤ kn ≤ 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

Northeastern Europe 2005, Northern Subregion
 
二分平均值
x为平均值,判断的时候 计算每个数的 (v[i] - w[i] * x) ,排序,找出最大的k个数,加起来,如果大于等于0 则往上找,否则往下
 
 #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的更多相关文章

  1. poj 3111 K Best 最大化平均值 二分思想

    poj 3111 K Best 最大化平均值 二分思想 题目链接: http://poj.org/problem?id=3111 思路: 挑战程序竞赛书上讲的很好,下面的解释也基本来源于此书 设定条件 ...

  2. POJ 3111 K Best(01分数规划)

    K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 9876   Accepted: 2535 Case Time ...

  3. POJ 3111 K Best(二分答案)

    [题目链接] http://poj.org/problem?id=3111 [题目大意] 选取k个物品,最大化sum(ai)/sum(bi) [题解] 如果答案是x,那么有sigma(a)>=s ...

  4. Divide and conquer:K Best(POJ 3111)

     挑选最美的珠宝 题目大意:挑选k个珠宝使得∑a/∑b最大,输出组合数 最大化平均值的标准题型,二分法就好了,一定要注意范围(10e-7),如果是10e-8就会tle,10e-6就是wa #inclu ...

  5. poj 3111 K Best (二分搜索之最大化平均值之01分数规划)

    Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...

  6. POJ 3111 K Best(最大化平均值)

    题目链接:click here~~ [题目大意]有n个物品的重量和价值各自是Wi和Vi.从中选出K个物品使得单位重量的价值最大,输出物品的编号 [解题思路]:最大化平均值的经典.參见click her ...

  7. POJ 3111 K Best

    二分,排序,贪心. 最优比率生成树,可以二分$+$贪心来实现,不过这样做精度不行. 如果是这样一个问题,该如何解决:问你$n$个里面选择$k$个,能否使得$\frac{{\sum\limits_{j ...

  8. K Besk [POJ 3111]

    描述 Demy有n颗宝石.她的每个珠宝都有一些价值vi和重量wi.自从丈夫约翰在最近的金融危机爆发后,已经决定出售一些珠宝.她决定自己会保留最好的珠宝.她决定保留这样的宝石,使他们的具体价值尽可能大. ...

  9. POJ - 3111 K Best 0-1分数规划 二分

    K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 12812   Accepted: 3290 Case Time ...

随机推荐

  1. java虚拟机理解探索1

    以下内容源于个人对<深入java虚拟机>的理解总结 基本概念: java虚拟机可以指一种抽象规范,也可以指一种具体实现,亦可以指一个java虚拟机实例. 虚拟机生命周期: 一个java虚拟 ...

  2. Codevs 1371 浴火银河跑运输

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold  题目描述 Description: 小 K 又在玩浴火银河了...不过这次他的目的真的是跑运输赚钱... 他想知 ...

  3. Css调整字体间距

    在span div 某些元素中有时候会用到调整字体的间距,设置方法: letter-spacing:15px;  

  4. Android app version code and name

    android:versionCode和android:versionName 区别   Android的版本可以在androidmainfest.xml中定义, 主要有android:version ...

  5. 【Qt】QSettings读写注册表、配置文件【转】

    简述 一般情况下,我们在开发软件过程中,都会缓存一些信息到本地,可以使用轻量级数据库sqlite,也可以操作注册表.读写配置文件. 关于QSettings的使用前面已经介绍过了,比较详细,见“更多参考 ...

  6. gulp插件

    gulp是趋势 gulp完全开发指南 => 快来换掉你的Grunt吧 gulp的工作流程:文件流--文件流--文件流......因为grunt操作会创建临时文件,会有频繁的IO操作,而gulp使 ...

  7. logstash 统计告警

    在实际的项目中需要对线上日志做实时分析跟统计,这一套方案可以用现有的ELK(ElasticSearch, Logstash, Kibana)方案既可以满足,关于这个方案的具体的步骤可以参考网上的解决方 ...

  8. cordova ios

    使用Cordova进行iOS开发 (环境配置及基本用法) 字数1426 阅读3044 评论0 喜欢5 安装Cordova CLI 1. cordova的安装: 1.1 安装cordova需要先安装no ...

  9. 通过数据绑定模板得到对应的Item控件

    这类控件都继承于Selector,其中主要有ComboBox.listview.listbox.datagrid. 由于个人对WPF的了解所有可能有遗漏,希望各位能够指出一起进步. 在遍历上面控件时主 ...

  10. 1014. Waiting in Line (30)

    Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...