【发布时间】:2021-08-25 03:40:34
【问题描述】:
问题描述:
假设农场上有n 圣诞树。您希望最大限度地提高所有树木都准备好迎接圣诞节的机会。为了让树木长得更快,你可以用m袋肥喂它们。
你已经根据你施肥的袋子数计算了每棵树按时准备好的概率。您有一个列表p[i][j] 表示如果施用了j 袋肥料,植物i 将准备好的概率。你不能把袋子分开,一旦一袋肥料施到一棵树上,你就不能将它用于不同的树。
施肥越多,树木并不总是生长得越快,因此随着施肥数量的增加,概率可能会减少或增加。
找出所有树木在圣诞节前完全长成的最大概率。
问题:
如何使用动态规划制表解决它?请编写一个带有 3 个参数的函数best_allocation(number_of_trees, number_of_bags, probability_list)。理想情况下,此函数通过优化分配肥料袋给植物,返回所有圣诞树为圣诞节做好准备的最高概率。
测试用例:
probs= [[0.5, 0.5, 1],[0.25,0.1,0.75]]
best_allocation(2,2,probs)
#In this case, the best choice is to allocate 0 bags to tree0, and allocate 2 bags to tree1,
#which gives us an overall probability of 0.75*0.5 = 0.375
probs = [[0.5, 0.75, 0.25],[0.75,0.25,0.8]]
best_allocation(2,2,probs)
#In this case, the best choice is to allocate 1 bags to plant0, and allocate 0 bags to plant1.
#The overall probability is 0.75*0.75=0.5625
【问题讨论】:
-
嗨@Ning!你能提供一个minimal reproducable example吗?你的目标描述得很好,但我们需要看看你的问题出现在哪里。
-
请注意,在 python 中,cmets 以
#开头,而不是//
标签: python dynamic-programming