[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 ...
随机推荐
- 执行超过1个小时的SQL语句
SELECT MO.MO_ID, MO.ITEM, MO.QTYORDERED, MO.PLANNEDSTARTDATE, BR.MAXLOTSIZE FROM TEMP_MO MO, (SELECT ...
- nullcon HackIM 2016 -- Crypto Question 1
You are in this GAME. A critical mission, and you are surrounded by the beauties, ready to shed thei ...
- MongoDB Replica Set 选举过程
什么是选举? 选举是副本集选择某个成员成为primary的过程.primary是一个副本集中唯一能够接收写操作的成员. 下面的事件能够引发一次选举: 第一次初始化一个副本集 Primary失效.rep ...
- git 配置忽略文件(忽略UserInterfaceState.xcuserstate,Breakpoints_v2.xcbkptlist)
ios 配置忽略文件.gitignore 文件 之前新建了一个项目,在使用git管理版本的时候没有配置忽略文件 .gitignore 文件,结果导致每次提交的时候都会出现UserInterfaceSt ...
- 安装运行okvis
1. 安装依赖项 sudo apt-get install cmake //cmake sudo apt-get install libgoogle-glog-dev // glog是Googl ...
- iOS 10 开发 相机相关的适配
升级 iOS 10 之后目测坑还是挺多的,记录一下吧,看看到时候会不会成为一个系列. 直入正题吧 今天在写 Swift 3 相关的一个项目小小练下手,发现调用相机,崩了.试试看调用相册,又特么崩了.然 ...
- git 调用 Beyond Compare
转载自 http://www.jackness.org/2015/03/31/git-%E8%B0%83%E7%94%A8-%E7%AC%AC%E4%B8%89%E6%96%B9%E5%AF%B9% ...
- 添加了有道生词本的 chrome google翻译扩展和有道翻译扩展
在chrome发布项目,需要先花美金认证,还得要美国ID,无奈. 直接上源码,需手动导入. 原始项目源码并未开源,个人是从chrome本地文件里拿出来的,拓展来的,侵删(本来想着自已写一个,业余时间, ...
- Gson实现自定义解析json格式
客户端跟服务器交互的时候我们使用json实现 但是 在交互的时候除了传送json对象数据意外 我们还需要传输标志位等 比如我们现在的交互方式格式 对象 { "data": { &q ...
- 详解C语言的类型转换
1.自动类型转换 字符型变量的值实质上是一个8位的整数值,因此取值范围一般是-128-127,char型变量也可以加修饰符unsigned,则unsigned char 型变量的取值范围是0-255( ...