K Best
Time Limit: 8000MS   Memory Limit: 65536K
Total Submissions: 9876   Accepted: 2535
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

Source

题目链接:POJ 3111

这题多了一个得输出方案序号,用结构体记录一下id就行,代码的话迭代或者二分都可以,但是迭代速度快很多很多

迭代代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 100010;
const double eps = 1e-9;
struct info
{
double w, v, d;
int id;
bool operator<(const info &rhs)const
{
return d > rhs.d;
}
};
info A[N]; double getnewk(int n, int k, double r)
{
double V = 0.0, W = 0.0;
for (int i = 0; i < n; ++i)
A[i].d = A[i].v - r * A[i].w;
sort(A, A + n);
for (int i = 0; i < k; ++i)
{
V += A[i].v;
W += A[i].w;
}
return V / W;
}
int main(void)
{
int n, k, i;
while (~scanf("%d%d", &n, &k))
{
for (i = 0; i < n; ++i)
{
scanf("%lf%lf", &A[i].v, &A[i].w);
A[i].id = i + 1;
}
double ans = 1, temp = 1;
while (1)
{
temp = getnewk(n, k, ans);
if (fabs(temp - ans) < eps)
break;
ans = temp;
}
for (i = 0; i < k; ++i)
printf("%d%s", A[i].id, i == k - 1 ? "\n" : " ");
}
return 0;
}

POJ 3111 K Best(01分数规划)的更多相关文章

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

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

  2. POJ 2976 Dropping tests 01分数规划

    给出n(n<=1000)个考试的成绩ai和满分bi,要求去掉k个考试成绩,使得剩下的∑ai/∑bi*100最大并输出. 典型的01分数规划 要使∑ai/∑bi最大,不妨设ans=∑ai/∑bi, ...

  3. POJ 2976 Dropping tests 01分数规划 模板

    Dropping tests   Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6373   Accepted: 2198 ...

  4. POJ 2728 Desert King (01分数规划)

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions:29775   Accepted: 8192 Descr ...

  5. $POJ$2976 $Dropping\ tests$ 01分数规划+贪心

    正解:01分数规划 解题报告: 传送门! 板子题鸭,,, 显然考虑变成$a[i]-mid\cdot b[i]$,显然无脑贪心下得选出最大的$k$个然后判断是否大于0就好(,,,这么弱智真的算贪心嘛$T ...

  6. POJ - 2976 Dropping tests(01分数规划---二分(最大化平均值))

    题意:有n组ai和bi,要求去掉k组,使下式值最大. 分析: 1.此题是典型的01分数规划. 01分数规划:给定两个数组,a[i]表示选取i的可以得到的价值,b[i]表示选取i的代价.x[i]=1代表 ...

  7. POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)

    [题意]每条路径有一个 cost 和 dist,求图中 sigma(cost) / sigma(dist) 最小的生成树. 标准的最优比率生成树,楼教主当年开场随手1YES然后把别人带错方向的题Orz ...

  8. POJ3111 K Best —— 01分数规划 二分法

    题目链接:http://poj.org/problem?id=3111 K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissio ...

  9. POJ 3621 Sightseeing Cows 01分数规划,最优比例环的问题

    http://www.cnblogs.com/wally/p/3228171.html 题解请戳上面 然后对于01规划的总结 1:对于一个表,求最优比例 这种就是每个点位有benefit和cost,这 ...

  10. POJ 2728 Desert King 01分数规划,最优比率生成树

    一个完全图,每两个点之间的cost是海拔差距的绝对值,长度是平面欧式距离, 让你找到一棵生成树,使得树边的的cost的和/距离的和,比例最小 然后就是最优比例生成树,也就是01规划裸题 看这一发:ht ...

随机推荐

  1. PAT (Basic Level) Practise (中文)- 1004. 成绩排名 (20)

    http://www.patest.cn/contests/pat-b-practise/1004 读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入 ...

  2. install ipython-notebook

    http://it.010lm.com/os/LINUX/182036.html ipython[notebook]安装(Linux平台) 1. 环境 操作系统:ubuntukylin 2. 操作步骤 ...

  3. 03_14_final关键字

    03_14_final关键字 1. Final关键字 final的变量的值不能够被改变 final的成员变量 final的局部变量(形参) final的方法不能够被重写 final的类不能够被继承

  4. java 实现猜数字游戏 随机给定一个数字,猜大小直到正确

    package com.swift; import java.util.Random; import java.util.Scanner; public class GuessBigSmall { p ...

  5. 问题008:java 中代码块的风格有几种?单行注释可否嵌套?多行注释可否嵌套?

    有两种:一种是次行风格,英文称为next-line 一种是是行尾风格,英文称为 end-of-line 举例 行尾风格 public class HelloWorld{ public static v ...

  6. Mybatis 插入一条或批量插入 返回带有自增长主键记录

    首先讲一下,  插入一条记录返回主键的 Mybatis 版本要求低点,而批量插入返回带主键的 需要升级到3.3.1版本,3.3.0之前的都不行, <dependency> <grou ...

  7. Linux常用关机重启命令

    # shutdown -h 10       //计算机将在10分钟后 关机,且会显示在登录用户的当前屏幕中 # shutdown -h now    //立即 关机 # shutdown -h 20 ...

  8. 二、Linux 系统启动过程

    Linux 系统启动过程 linux启动时我们会看到许多启动信息. Linux系统的启动过程并不是大家想象中的那么复杂,其过程可以分为5个阶段: 内核的引导. 运行 init. 系统初始化. 建立终端 ...

  9. Python 文件读写 文件和路径

    1.在Windows上,使用倒斜杆作为文件夹之间的分隔符,在Linux上,使用正斜杠作为路径分隔符.在编写Python脚本时,可以os.path.join()函数来处理 在Windows环境下命令如下 ...

  10. 静态属性property的本质和应用

    一.本质 静态属性property本质就是实现了get,set,delete三种方法 class Foo: @property def AAA(self): print('get的时候运行我啊') @ ...