美文网首页
书本分发

书本分发

作者: loick | 来源:发表于2019-11-26 15:59 被阅读0次

Description

You are given N number of books. Every ith book has Pi number of pages. You have to allocate books to M number of students. There can be many ways or permutations to do so. In each permutation one of the M students will be allocated the maximum number of pages. Out of all these permutations, the task is to find that particular permutation in which the maximum number of pages allocated to a student is minimum of those in all the other permutations, and print this minimum value. Each book will be allocated to exactly one student. Each student has to be allocated atleast one book.

思路

一个典型的动态规划,尝试分配1|2|3...|n-1个任务给当前的人,再递归剩下的任务和人数

  1. 假设当前分配x个任务给第一个人,那么分配方式的结果是sum(task1, task2...task_x)和子问题f(n-x, people_nums-1),两者中的最大值。

  2. 对上述的不同x,找出对应分配方式的最小值返回

递推公式:

f(n, p) = min(max(sum[task_1~task_i], f(n-i, p-1)), 1 <= i <= n
递归结束条件是n == 0(没有任务)或者 p==1(只剩下一个人可以分配任务)

时间复杂度O(n^2)

python
from functools import lru_cache
def solve(p, tasks):
    @lru_cache(None)
    def dfs(rp, it):
        if rp == 1:
            return sum(tasks[it:])
        if it == len(tasks) - 1:
            return tasks[-1]
        rp -= 1

        ans, cursum = float('inf'), 0
        for j in range(it, len(tasks)):
            cursum += tasks[j]
            ans = min(ans, max(cursum, dfs(rp, j+1)))
        return ans

    return dfs(p, 0)

相关文章

  • 书本分发

    Description You are given N number of books. Every ith bo...

  • 海叙

    学生时期,记忆中的大海,最深刻是高尔基《海燕》中关于海的描述。那时,海遥远而神秘,只能通过书本充分发挥想象力。作为...

  • 书本

    书本关于历史,。它里面有很多知识,有些书不是关于历史的。它是关于其他的。它属于快乐。我们要多认字。要学会看书。在书...

  • 书本

    有些东西,往心里去了 没能跑到纸上来 那一段段推出去的故事 被时间衔在嘴里 你不问,我不说 皮筋随手摸来 一把扎起...

  • 书本

    年轻的幼子啊,无论你做些什么旁人皆不会有所苛责,可有些东西却是不可为之的,有些底线是不能逾越的。我并不生气...

  • 《书本》

    【有一天希望我出版的书, 字迹不要太大, 也不要太小, 以便于一些开始衰老的人也能读它, 行距要适...

  • 书本

    你是智慧的代表, 你是勤劳的园丁。 你把知识浇灌给花朵, 你是幽默的老人, 把笑话念给我们听。 你是满肚子墨水的解...

  • 书本

    打开抖音,本想查一下快递情况,结果不知不觉又刷了俩小时,有点困了,不看了,越看越想看,啥也记不住,也没有啥营养,说...

  • 书本

    我想大家应该都看过书吧!书有很多种,每个人都有一种自己喜欢看的书,今天我来给大家分享一本书,它的名字叫《诸...

  • 书本

    在一个酒店的大堂 看到隔断柜上躺着几十本书 她们在各种商品的挤压下 拥有很小的一席之地 去阅读和欣赏的人肯定不多 ...

网友评论

      本文标题:书本分发

      本文链接:https://www.haomeiwen.com/subject/zqfywctx.html