高级农民
- 积分
- 1277
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2014-11-15
- 最后登录
- 1970-1-1
|
- from typing import List
- from heapq import heapify, heappop, heappush
- class Solution:
- def cal_min_trucks(
- self,
- quantities: List[int],
- truck_cap: int,
- max_parcel_per_type: int,
- ) -> int:
- hq = [-q for q in quantities]
- heapify(hq)
- res = 0
- while hq:
- load = 0
- tmp_for_used_type = []
- while load < truck_cap and hq:
- # choose the type with the most parcel to put into the current truck
- curr_type_cnt = heappop(hq)
- to_put = min(-curr_type_cnt, truck_cap - load, max_parcel_per_type)
- type_remaining = -curr_type_cnt - to_put
- # cache the remainings and reput them back for the next round, you can not put them back to hq now to avoid next round of the hq push pick this type back again
- if type_remaining:
- tmp_for_used_type.append(-type_remaining)
- # update the status of parcel and truck
- load += to_put
- # now we finished loading the current truck, update the parcel status and truck status
- res += 1
- for i in tmp_for_used_type:
- heappush(hq, i)
- return res
- if __name__ == '__main__':
- size_q = int(input().strip())
- quantities = []
- for _ in range(size_q):
- quantities.append(int(input().strip()))
- truck_cap = int(input().strip())
- max_parcel_per_type = int(input().strip())
- print(Solution().cal_min_trucks(quantities, truck_cap, max_parcel_per_type))
-
复制代码 |
|