[UCSD白板题] Fractional Knapsack
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的更多相关文章
- [UCSD白板题] Take as Much Gold as Possible
Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- [UCSD白板题] Maximize the Value of an Arithmetic Expression
Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
- [UCSD白板题] Primitive Calculator
Problem Introduction You are given a primitive calculator that can perform the following three opera ...
- [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 ...
- [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 ...
- [UCSD白板题] Sorting: 3-Way Partition
Problem Introduction The goal in this problem is to redesign a given implementation of the randomize ...
- [UCSD白板题] Majority Element
Problem Introduction An element of a sequence of length \(n\) is called a majority element if it app ...
随机推荐
- frame busting
[frame busting] 参考:http://book.51cto.com/art/201204/330076.htm
- 第一次将内容添加到azure event hubs
由于每秒数据吞吐量巨大,需要将实时数据存到event hubs,再由event hubs定时定量保存到document DB. event hubs的介绍详见微软官页:https://azure.mi ...
- otter双主同步安装与配置
otter是阿里的开源数据同步项目,资源地址就不用说了哈,网上找,阿里云论坛关于单方向同步的配置已经很清楚了,理论上说,双主同步也不复杂,但是毕竟 是数据库,比较重要,配置双主的时候,总觉得心里没底, ...
- setValuesForKeysWithDictionary 的用法
字典的快速赋值 setValuesForKeysWithDictionary 字数918 阅读6604 评论6 喜欢32 前言 在学习解析数据的时候,我们经常是这么写的:PersonModel.h文件 ...
- start
------siwuxie095 start 启动另一个窗口运行指定的程序或命令 语法: START ["title"] [/D path] [/I] [/MIN] [/MAX] ...
- javascript:history.go()和History.back()的区别
http://www.mikebai.com/Article/2009-11/757.html
- 安装好grunt,cmd 提示"grunt不是内部或外部命令" 怎么办?
Grunt和所有grunt插件都是基于nodejs来运行的,因此,必须安装node.js. (一) 去官网http://nodejs.org/ 下载安装包 node-v6.9.2.msi,直接点击安装 ...
- java项目中build path的设置
右键点击项目新建文件libs 添加jtds jar包引用本地动态链接库(dll)的设置方法 配置LibraryJRE的添加和更换 Java项目中build path的设置总结,包括JRE的添加和更 ...
- Java(数组)动手动脑
1>数组作为方法参数 阅读并运行示例PassArray.java,观察并分析程序输出的结果,小结,然后与下页幻灯片所讲的内容进行对照. 源代码: // PassArray.java // Pas ...
- 更快学习 JavaScript 的 6 个思维技巧
更快学习 JavaScript 的 6 个思维技巧 我们在学习JavaScript,或其他任何编码技能的时候,往往是因为这些拦路虎而裹足不前: 有些概念可能会造成混淆,尤其当你是从其他语言转过来的时候 ...