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. Spark RDD整理

    参考资料: Spark和RDD模型研究:http://itindex.net/detail/51871-spark-rdd-模型 理解Spark的核心RDD:http://www.infoq.com/ ...

  2. 比较不错的一个ios找茬游戏源码

    找茬游戏源码 ,这个是一款非常不错的ios找茬游戏源码,该游戏的兼容性非常好的,并且还可以支持ipad和iphone,UI界面设计得也很漂亮,游戏源码真的是一款非常完美,而且又很完整的一款休闲类的游戏 ...

  3. ▲教你如何轻易的做linux计划任务▲——小菜一碟

    一次性计划任务的安排: at :安排作业在某一时刻执行一次(一般都是用它) batch:安排作业在系统负载不重时执行一次 第一步: #service atd start  开启一次性计划任务   at ...

  4. Hadoop上路-04_HBase0.98.0入门

    以下操作在Hadoop分布式集群基础上进行. 一.分布式环境搭建 下载:)验证 3)修改%HBASE%/conf/hbase-env.sh 4)修改$HBASE_HOME/conf/hbase-sit ...

  5. js禁止页面复制 禁用页面右键菜单的代码

    js实现禁止页面复制功能.禁用页面右键菜单等功能.  <body oncontextmenu="return false">禁用网页右键菜单,但是仍然可以使用快捷键复制 ...

  6. 关于JS异步加载方案

    javascript延迟加载的解决方案: 1.使用defer标签 <span style="font-size: small;"><script type=&qu ...

  7. MySQL实战积累

    IFNULL(expr1,expr2)的用法:假如expr1不为NULL,则IFNULL()的返回值为   expr1; 否则其返回值为expr2. 索引:http://www.cnblogs.com ...

  8. openerp 经典收藏 通过view实现字段的只读、隐藏操作(转载)

    通过view实现字段的只读.隐藏操作 原文地址:http://cn.openerp.cn/view_groups/ 在OpenERP V7视图(ir.ui.view)多了一个非常有用的字段(group ...

  9. Microsoft .NET Framework 3.5 for Windowns Server2012R2 GUI

    图形化安装,需要安装盘,不需要网络连接

  10. ubuntu 下dbus的环境搭建和使用

    从https://launchpad.net/ubuntu/+source/dbus/1.10.6-1ubuntu2下载需要的dbus包,然后解压,./configure make && ...