当前位置:网站首页>leetcode:857. 雇佣 K 名工人的最低成本【分块思考 + 由简单入手】
leetcode:857. 雇佣 K 名工人的最低成本【分块思考 + 由简单入手】
2022-07-26 21:33:00 【白速龙王的回眸】

分析
假设选了k个人是x1 … xk
他们对应的q和w是q1…qk;w1…wk
实际的收入是r1…rk;
我们知道ri = qi * t(t是比例系数)
然后必须有ri >= wi 因此t >= wi / qi
所以最贪心的话 t = max(wi / qi)
特别的,我们令t = [wage[i] / quality[i] for i in range(n)]
因此我们抽k个人出来
就是要求sum(qi) * max(ti)
显然max好求,我们可以对t排个序
然后当固定了max t之后,qi就被限制了范围,这样的话用个sortedlist维护前k大的即可搞定
ac code
from sortedcontainers import SortedList
class Solution:
def mincostToHireWorkers(self, quality: List[int], wage: List[int], k: int) -> float:
# 实际工资之比 = 质量之比
# 实际工资 >= 理想工资
# 最小总工资
n = len(quality)
t = [wage[i] / quality[i] for i in range(n)]
# 从q,t选k个使得 sum(qi) * max(ti) 最小,多变量使用固定一个的思想
# max(ti)好固定
# sum(qi)选前面的k个使得和最少,sortedlist?
sorted_t_q = sorted([(tt, qq) for tt, qq in zip(t, quality)])
#print(sorted_t_q)
ans = inf
stl = SortedList([])
# init
for i in range(k):
stl.add(sorted_t_q[i][1])
ans = min(ans, sum(stl[:k]) * sorted_t_q[k - 1][0])
# next
for i in range(k, n):
stl.add(sorted_t_q[i][1])
ans = min(ans, sum(stl[:k]) * sorted_t_q[i][0])
return ans
总结
两个量的问题不好求
先固定一个,然后再求另一个
一般来说,先固定简单的那个
边栏推荐
- 调试stc8a8k64d4单片机485通信总结
- Is it safe to open an account on flush? How to choose a securities firm for opening an account
- 08.02 adjacency table
- Get network time by unity
- Altium Designer 22 中文字符乱码
- Try new functions | decrypt Doris complex data type array
- 同花顺手机炒股开户安全吗?怎么办理开户呢
- unity 获取网络时间
- July training (the 26th day) - and check the collection
- win10下mysql改密码后,突然登录不上的问题解决过程记录
猜你喜欢

Altium Designer 22 中文字符乱码

08.02 邻接表

09.01 depth first search

Pytoch -- used by visdom

调试stc8a8k64d4单片机485通信总结

08 du 命令

Triangular wave spectrum of MATLAB excitation model

Add resource files for the project and pictures for buttons in QT

My SQL is OK. Why is it still so slow? MySQL locking rules

Basic operation of (C language) files
随机推荐
Do you know why to design test cases after learning so long about use case design
matlab 画短时能量图
Let Xiaobai thoroughly understand performance tuning
When deploying Flink on a single machine and creating the connection table of oracle19c RAC, the error ora-12505 is reported. Who can help
Go----Go 语言中的标识符和关键字
C data type_ From rookie tutorial
[waiting and wakeup of QT multithreaded threads]
EasyUI DataGrid obtains multiple selected data for operation
Triangular wave spectrum of MATLAB excitation model
View drawing process 1-the relationship between view and window
ZABBIX calls API retrieval method
What is Base64?
JWT implements login authentication + token automatic renewal scheme, which is the correct posture!
JMeter自定义日志与日志分析
View绘制流程1-View与Window的关系
Go -- go language naming specification
09.01 深度优先搜索
Just one dependency to give swagger a new skin, which is simple and cool
09.01 depth first search
easyui datagrid 获取多条选中的数据进行操作