- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我可以解决Copying Books Problem使用二进制搜索方法,因为它很容易实现。但是我刚刚开始解决动态规划问题,我想知道该问题的动态规划解决方案
Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scribers. The scriber had been given a book and after several months he finished its copy. One of the most famous scribers lived in the 15th century and his name was Xaverius Endricus Remius Ontius Xendrianus (Xerox). Anyway, the work was very annoying and boring. And the only way to speed it up was to hire more scribers.
Once upon a time, there was a theater ensemble that wanted to play famous Antique Tragedies. The scripts of these plays were divided into many books and actors needed more copies of them, of course. So they hired many scribers to make copies of these books. Imagine you have m books (numbered 1, 2, ...., m) that may have different number of pages ( p_1, p_2, ..., p_m) and you want to make one copy of each of them. Your task is to divide these books among k scribes, k <= m. Each book can be assigned to a single scriber only, and every scriber must get a continuous sequence of books. That means, there exists an increasing succession of numbers 0 = b_0 < b_1 < b_2, ... < b_{k-1} <= b_k = m$ such that i-th scriber gets a sequence of books with numbers between bi-1+1 and bi. The time needed to make a copy of all the books is determined by the scriber who was assigned the most work. Therefore, our goal is to minimize the maximum number of pages assigned to a single scriber. Your task is to find the optimal assignment.
对于二进制搜索,我正在执行以下操作。
Low =1 and High = Sum of pages of all books
Run Binary search
For Mid(Max pages assigned to a scribe), assign books greedily such that no scribe gets page more than MAX
If scribes remain without work it means actual value is less than MID, if Books remain actual pages is more MID and I am updating accordingly.
最佳答案
这是一个用 python 编写的可能的动态编程解决方案。我使用从 0 开始的索引。
k = 2 # number of scribes
# number of pages per book. 11 pages for first book, 1 for second, etc.
pages = [11, 1, 1, 10, 1, 1, 3, 3]
m = len(pages) # number of books
def find_score(assignment):
max_pages = -1
for scribe in assignment:
max_pages = max(max_pages, sum([pages[book] for book in scribe]))
return max_pages
def find_assignment(assignment, scribe, book):
if book == m:
return find_score(assignment), assignment
assign_current = [x[:] for x in assignment] # deep copy
assign_current[scribe].append(book)
current = find_assignment(assign_current, scribe, book + 1)
if scribe == k - 1:
return current
assign_next = [x[:] for x in assignment] # deep copy
assign_next[scribe + 1].append(book)
next = find_assignment(assign_next, scribe + 1, book + 1)
return min(current, next)
initial_assignment = [[] for x in range(k)]
print find_assignment(initial_assignment, 0, 0)
函数 find_assignment 返回分配列表,其中第 i 个元素是分配给第 i 个抄写员的书籍索引列表。作业的分数也会返回(抄写员必须在作业中复制的最大页数)。
动态规划的关键是首先识别子问题。在这种情况下,书籍是有序的,只能按顺序分配。因此,子问题是使用 s 个抄写员(其中 n < m 且 s < k)为最后 n 本书找到最佳分配。一个子问题可以使用以下关系用更小的子问题来解决:min(将书分配给“当前”抄写员,将书分配给下一个抄写员)。
关于algorithm - 抄书 UVa Online Judge 动态规划解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23680188/
发表时间:2024 期刊会议:arxiv 论文单位:Arizona State University 论文作者:Dawei Li, Bohan Jiang, Liangjie Hua
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 4 年前。 Improve th
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我正在研究问题 00156,Anagrams。 我编译了代码,它在我这边工作得很好,但是当我上传代码时,它在在线判断上返回运行时错误。 我想了解错误在哪里,但我对 C++ 很陌生,所以请保持简单,提前
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我在看问题Magic Square我确信有一些循环,如果条件可以解决这个问题,但我很想知道是否有任何已知的算法/数据结构来解决这个问题。我对精确的解决方案不感兴趣,但对算法/数据结构的任何提示都会有所
我对竞争性编程还很陌生,我已经在 CodeForces 中遇到过几次这个问题,但我不太确定发生了什么。 问题是,当我运行这段代码时(来自 http://codeforces.com/contest/1
我需要有关 Uri Online Judge 网站中的逻辑数学问题的帮助: 提交的代码以“var n”开始,我使用 var 行来处理输入示例: var n; var j; var n = parseI
这些是关于 codechef 的问题的链接和 UVa online Judge 我的C代码是这样的: #include int main() { char c; int n,m; char d; i
我正在使用 Judge gem 构建简单的客户端验证。 在我选择对“blur”而不是按钮点击运行验证之前,一切都很完美。 脚本: var divs = ["advertisement_age","ad
问题是这样的..输入 n [the number of multiplications #include using namespace std; int main() { long int
描述:对于由多个非负整数组成的数组,数组归一化意味着每个元素都将除以数组的总和。假设数组至少由一个元素组成,并且元素的总和不会超过诠释。 输入:几个非负整数 输出:归一化的结果。 示例输入1 2 3
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我可以解决Copying Books Problem使用二进制搜索方法,因为它很容易实现。但是我刚刚开始解决动态规划问题,我想知道该问题的动态规划解决方案 Before the invention o
这个问题在这里已经有了答案: How to verify if a file exists in a batch file? (3 个答案) 关闭 8 年前。 伪代码: if file exists
我正在解决JAVA编程挑战中的鄂尔多斯数问题。该代码在我的机器上完美运行。然而在线判断会导致运行时错误。谁能指出我犯的错误吗? http://uva.onlinejudge.org/index.php
我正在尝试做题http://acm.timus.ru/problem.aspx?space=1&num=1119在 Timus Online Judge 上。但是,由于某些奇怪的原因,递归函数不起作用
我正在对法国的法官任命系统进行一些数据分析,并使用一些代码来执行此操作。但我遇到了正则表达式问题。 我的数据由一大堆法院和法官任命组成。 这是一个示例列表: Appellate Court of Pa
我试图在 UVa ( http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=sho
这个问题困扰我好久了。它必须具有动态编程解决方案,因为它已被标记为“动态编程”。请提出一个方法。 Question Link 简化的问题陈述: There are 3 islands having N
我是一名优秀的程序员,十分优秀!