美文网首页
订单问题

订单问题

作者: loick | 来源:发表于2019-11-25 22:51 被阅读0次

订单问题

Description

Rahul and Ankit are the only two waiters in Royal Restaurant. Today, the restaurant received N orders. The amount of tips may differ when handled by different waiters, if Rahul takes the ith order, he would be tipped Ai rupees and if Ankit takes this order, the tip would be Bi rupees.In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than X orders and Ankit cannot take more than Y orders. It is guaranteed that X + Y is greater than or equal to N, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders.

(分配订单,A,B可以接固定数量的订单,每个订单A,B完成的价值不同,求最大分配价值)

思路
  1. 在geek上看到一个O(nlogn)的解法,非常直接,对订单进行排序(订单对A的价值越大,对B的价值越小排前面,也就是说排序key= -(valueA[i]-valueB[i])),然后顺序分配订单给A,直到A的订单满了或者A完成当前订单的价值小于B时停止,剩下的订单分配给B。

2.就是动态规划

从第一个订单开始,把订单分配给A,或者B,递归剩下的订单,取两种分配的最大值。

`ans = max(valA[i] + f(i+1, ra-1, rb), valB[i] + f(i+1, ra, rb-1))`

其中valA表示A的价值数组,ra表示A还可以完成的订单,i表示当前任务index
参考实现(O(n^3))
from functools import lru_cache
def solve(X, Y, tx, ty):
    @lru_cache(None)
    def dfs(x, y, i):
        if i == len(tx):
            return 0
        if x == 0:
            return sum(ty[i:])
        elif y == 0:
            return sum(tx[i:])
        return max(tx[i]+dfs(x-1, y, i+1), ty[i] + dfs(x, y-1, i+1))
    return dfs(X, Y, 0)
完整代码

O_NJU_JK

相关文章

  • 订单问题

    订单问题 Description Rahul and Ankit are the only two waiters...

  • 如何防止订单重复提交以及如何防止订单重复确认

    订单重复问题已经是老生常谈的问题了,我们熟悉的淘宝购物流程,购物车-->生成订单--》提交订单--》订单确认--》...

  • 如何防止订单重复提交以及如何防止订单重复确认

    订单重复问题已经是老生常谈的问题了,我们熟悉的淘宝购物流程,购物车-->生成订单--》提交订单--》订单确认--》...

  • 工作笔记20171202

    强华订单问题,国恩售料审核问题! 反思,监控渠道!反思,订单需要转的问题! 关注,换型问题!

  • 重复使用优惠券问题

    问题说明:用户在下订单时会出现不同订单使用同一张优惠券的问题。问题复现:原业务逻辑为,用户下单后,先创建订单,再扣...

  • 问题处理列表

    1.【售后处理模板】 订单号: 问题描述: 订单截图: 受损问题图片: 联系人电话:

  • wangzx work report 16/4/12

    订单详情 修改了一下加菜显示不正确的问题 订单列表 修改了订单列表包括餐具时显示几道菜的问题

  • SPIN提问

    SPIN提问,区别于封闭式问题和开放式问题。 后者在小订单销售有作用,但在大订单销售中效果不佳。 大订单销售推荐使...

  • 2018-12-26

    小问题:管理员来终止订单的时候终止订单中的所有产品 前端:写个传订单号的方法,post提交 通过订单号查出订单明细...

  • 04-消息实现分布式事务

    问题 场景:订单系统创建订单成功,需购物车系统清空购物车中的订单 业务执行步骤: 在订单库中插入一条订单数据,创建...

网友评论

      本文标题:订单问题

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