Problem Introduction

Given a set of items and total capacity of a knapsack,find the maximal value of fractions of items that fit into the knapsack.

Problem Description

Task.The goal of this code problem is to implement an algorithm for the fractional knapsack problem.

Input Format.The first line of the input contains the number \(n\) of items and the capacity \(W\) of a knapsack.The next \(n\) lines define the values and weights of the items. The \(i\)-th line contain integers \(v_i\) and \(w_i\)—the value and the weight of \(i\)-th item,respectively.

Constraints.\(1 \leq n \leq 10^3, 0 \leq W \leq 2 \cdot 10^6; 0 \leq v_i \leq 2 \cdot 10^6, 0 < w_i \leq 2 \cdot 10^6\) for all \(1 \leq i \leq n.\) All the numbers are integers.

Output Format.Output the maximal value of fractions of items that fit into the knapsack.The absolution value of the difference between the answer with at least four digits after the decimal point(otherwise your answer,while being computed correctly,can turn out to be wrong because of rounding issues).

Sample 1.
Input:

3 50
60 20
100 50
120 30

Output:

180.0000

Sample 2.
Input:

1 10
500 30

Output:

166.6667

Solution

# Uses python3
import sys
import numpy as np

def get_optimal_value(capacity, weights, values):
    value = 0.
    indices = np.argsort([-v/w for w,v in zip(weights,values)])
    for idx in indices:
        if capacity <= 0:
            break
        weight = min(capacity, weights[idx])
        capacity -= weight
        value +=  weight * (values[idx] / weights[idx])
    return value

if __name__ == "__main__":
    data = list(map(int, sys.stdin.read().split()))
    n, capacity = data[0:2]
    values = data[2:(2 * n + 2):2]
    weights = data[3:(2 * n + 2):2]
    opt_value = get_optimal_value(capacity, weights, values)
    print("{:.10f}".format(opt_value))

[UCSD白板题] Fractional Knapsack的更多相关文章

  1. [UCSD白板题] Take as Much Gold as Possible

    Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...

  2. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  3. [UCSD白板题] Maximize the Value of an Arithmetic Expression

    Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...

  4. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  5. [UCSD白板题] Primitive Calculator

    Problem Introduction You are given a primitive calculator that can perform the following three opera ...

  6. [UCSD白板题] Points and Segments

    Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...

  7. [UCSD白板题] Number of Inversions

    Problem Introduction An inversion of a sequence \(a_0,a_1,\cdots,a_{n-1}\) is a pair of indices \(0 ...

  8. [UCSD白板题] Sorting: 3-Way Partition

    Problem Introduction The goal in this problem is to redesign a given implementation of the randomize ...

  9. [UCSD白板题] Majority Element

    Problem Introduction An element of a sequence of length \(n\) is called a majority element if it app ...

随机推荐

  1. 阿里巴巴Java招聘

    大家好: 我是阿里巴巴B2B的应用架构师,现在大量招聘Java工程师,对自己技术有信心的兄弟姐妹,请联系我吧. 版权声明:本文为博主原创文章,未经博主允许不得转载.

  2. 从西直门立交桥谈IT架构与重构(干货)

    2015年8月13日 PM 20:00 Neeke君从一个战场奔赴至另一个战场,回到办公室,打开电脑,登陆微信,精彩的的微社群分享马上就要开始了! 大家好,我是Neeke,中文名高驰涛,PHP开发组成 ...

  3. TSkinData 皮肤控件后最大最小提示英文Close的解决方法

    1.D:\soft\控件\VclSkin5.40-D7-D2010 New\source 控件安装位置 2.WinSkinForm.pas 查找Close 3.function TWinSkinFor ...

  4. Java中public,private,protected,和默认的区别

    Java中public,private,protected,和默认的区别 1.private修饰词,表示成员是私有的,只有自身可以访问: 2.protected,表示受保护权限,体现在继承,即子类可以 ...

  5. JSP(include指令与<jsp:include>动作的区别)

    <%@ page language= "java" contentType="text/html;charset=UTF-8" %><html ...

  6. [整理]S-Record数据格式解析

    S-Reord是一种由摩托罗拉公司创建的文件格式(不得不说,摩托罗拉厉害啊,SPI和S-Record都是他们创造的).S-Record的基本字符为ASCII字符,用以表示相应的十六进制数据.该数据格式 ...

  7. 如何用Unity制作自定义字体——Custom Font

    一.效果图 二.步骤 将美术做好的字体分块导入BMFont,使用BMFont工具生成艺术字库: 将上面的数据导入unity资源目录下:*.fnt文件中记录每个文字的状态信息: 导入*.png图片并设置 ...

  8. MySQL 数据库常用命令

    1.MySQL常用命令 create database name; 创建数据库 use databasename; 选择数据库 drop database name 直接删除数据库,不提醒 show ...

  9. code project 上的内存管理的示例代码

    /******************************************************************** created: 2014/03/17 18:53 file ...

  10. 【好文要转】HTTP图解(大牛必经之路)

    http://www.cnblogs.com/aylin/p/6221436.html