[LeetCode] 857. Minimum Cost to Hire K Workers 雇佣K名工人的最低成本

There are?N?workers.? The?i-th worker has a?quality[i]?and a minimum wage expectation?wage[i].

Now we want to hire exactly?K?workers to form a?paid group.? When hiring a group of K workers, we must pay them according to the following rules:

  1. Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group.
  2. Every worker in the paid group must be paid at least their minimum wage expectation.

Return the least amount of money needed to form a paid group satisfying the above conditions.

Example 1:

Input: quality = [10,20,5], wage = [70,50,30], K = 2
Output: 105.00000
Explanation: We pay 70 to 0-th worker and 35 to 2-th worker.

Example 2:

Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], K = 3
Output: 30.66667
Explanation: We pay 4 to 0-th worker, 13.33333 to 2-th and 3-th workers seperately.

Note:

  1. 1 <= K <= N <= 10000, where?N = quality.length = wage.length
  2. 1 <= quality[i] <= 10000
  3. 1 <= wage[i] <= 10000
  4. Answers within?10^-5?of the correct answer will be considered correct.

Github 同步地址:

https://github.com/grandyang/leetcode/issues/857

参考资料:

https://leetcode.com/problems/minimum-cost-to-hire-k-workers/

LeetCode All in One 题目讲解汇总(持续更新中...)

原文地址:https://www.cnblogs.com/grandyang/p/11329482.html

时间: 2024-11-08 08:00:06

[LeetCode] 857. Minimum Cost to Hire K Workers 雇佣K名工人的最低成本的相关文章

LeetCode 857. Minimum Cost to Hire K Workers

合法的pay group里所有worker的比例是一样的,即 wage1/wage2 = quality1/quality2 推出 wage1/quality1 = wage2/quality2. 这就好办了,定义 ratio_i = wage_i/quality_i.对于一个group,ratio一定是所有人中最大的那个. 对于一个大小为k的group,需要pay的钱就是 Σ_k quality_i * 最大的ratio. 为了避免每次扫一遍group找到最大的ratio,我们根据ratio排

857. Minimum Cost to Hire K Workers

有N个工人,对于每一个工人存在一个 quality 和 wage 要雇佣 K个 工人 雇佣的工人 必须满足 这两者的比值相同 1 注意到我们可以按照按照工人的比值进行排序 可以分解成若干个更小的有相互关联的子问题 前 K 个 可以进行选择的工人, 按照第K个 工人的比值 即可 但是我们要利用这里 这些子问题的相似性 前K 个工人和 前K +1 个工人 虽然使用的比值有可能不同 注意 : 从第K个工人到 K+1 个工人, 剔除了哪一个工人? 是q 最大的那个工人 因为最大的q 使得最后的花费最大

[LeetCode] 1000. Minimum Cost to Merge Stones

There are N piles of stones arranged in a row.  The i-th pile has stones[i] stones. A move consists of merging exactly K consecutive piles into one pile, and the cost of this move is equal to the total number of stones in these K piles. Find the mini

[LeetCode 1368] Minimum Cost to Make at Least One Valid Path in a Grid

Given a m x n grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign of grid[i][j] can be: 1 which means go to the cell to the right. (i.e go from grid[i][j] to grid[i][j + 1]) 2 

POJ 2516 Minimum Cost(最小费用最大流啊)

题目链接:http://poj.org/problem?id=2516 Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (marked from 1 to N) which stocks goods from him.Dearboy has M supply places (mar

POJ 2516 Minimum Cost (最小费用最大流)

POJ 2516 Minimum Cost 链接:http://poj.org/problem?id=2516 题意:有M个仓库,N个商人,K种物品.先输入N,M,K.然后输入N行K个数,每一行代表一个商人要购买的物品,其中K个数分别表示要购买的每件商品数.然后是M行K个数,每行表示仓库里的情况,其中K个数分别每种物品的库存量.接下来是K个矩阵,每个矩阵为N*M,分别表示第K种物品从M个仓库运到第N个商人的花费.问能否合理安排,使得花费最少,如果不行就输出-1. 思路: 一开始的时候,竟然构造了

POJ 2516 Minimum Cost

Minimum Cost Time Limit: 4000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 251664-bit integer IO format: %lld      Java class name: Main Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In hi

【POJ2516】Minimum Cost

[POJ2516]Minimum Cost 题意:有N个收购商.M个供应商.K种物品.对于每种物品,每个供应商的供应量和每个收购商的需求量已知.每个供应商与每个收购商之间运送该物品的运费已知.求满足收购商要求的前提下的最小运费.(n,M,K<=50,每种物品供求量<=3,单位运费<=100) 题解:怎么看怎么是最小费用流,但是刚学的KM算法,还是要用一下的~ 由于各种物品间没有影响,所以可以将k种物品拆开:由于供求量≤3,所以可以将供应商和收购商都拆开,然后跑KM算法. #include

Poj 2516 Minimum Cost (最小花费最大流)

题目链接: Poj  2516  Minimum Cost 题目描述: 有n个商店,m个仓储,每个商店和仓库都有k种货物.嘛!现在n个商店要开始向m个仓库发出订单了,订单信息为当前商店对每种货物的需求量.不同的商店从不同的仓库购买不同货物花费不同,问是不是能满足所有商店的要求,如果能商店花费总和最小为多少? 解题思路: 简单的费用流,要跑K次最小花费最大流,每次只对一种货物建图跑费用流.每次建图以0为源点, [1,m]为仓库, [m+1, n+m]为商店, n+m+1为汇点.0与[1,m]连边,