[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 ...
随机推荐
- linux命令(2):cd命令
Linux cd 命令可以说是Linux中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用 cd 命令上的. 所以,学习Linux 常用命令,首先就要学好 cd 命令的使用方法技巧. 1. ...
- python第十九天-----Django进阶
1.机智的小django为我你们提供了快捷的表单验证! from django.shortcuts import render, HttpResponse,redirect from django i ...
- 自适应UITableView的Cell高度问题
1.自己计算Cell的高度返回: 1>model中计算: // // InfoModel.h // OCDemo // // Created by 思 彭 on 16/12/27. // Cop ...
- exit
-------siwuxie095 三个退出命令: exit exit /b goto :eof 这三个命令都可以退出批处理,但之间有细微差别: (1)运行 GOTO :EOF 后,CMD返回并将等待 ...
- opencv单目摄像机标定(二)
// 引入实际标定板方格宽度的标定程序 #include <string> #include <iostream> #include <cv.h> #include ...
- laravel中日志为daily时如何设置最大保存天数
在laravel中,日志设置为daily时,默认保存七天的日志,超过则清除七天前的日志.可修改默认的设置,假如要保存30天的日志,则配置如下: 在配置文件config/app.php中添加如下代码: ...
- 根据第三方库spire.pdf使用指定打印机打印pdf文件
private void button1_Click(object sender, EventArgs e) { PdfDocument doc = new PdfDocument(); string ...
- Ubuntu15下mysql5.6.25解决不支持中文的办法
apt-get install 安装的,不是源码包安装的mysql 1 修改mysql的配置文件 /etc/mysql/conf.d/mysql.cnf 在[mysql]的下方加入如下语句:(注:这个 ...
- git 查看远程分支、本地分支、创建分支、把分支推到远程repository、删除本地分支
1 查看远程分支 $ git branch -a * br-2.1.2.2 master remotes/origin/HEAD -> origin/master remotes/origin/ ...
- get_object_vars($var) vs array($var)
get_object_vars(\(var) vs array(\)var) test case class Test { public function actionGetObjectVarsVsA ...