当前位置:网站首页>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
总结
两个量的问题不好求
先固定一个,然后再求另一个
一般来说,先固定简单的那个
边栏推荐
- C data type_ From rookie tutorial
- Redis distributed lock + Lua atomic operation
- Schematic diagram of MOS tube
- Join method in JS
- Finding a new direction for the development of digital retail is the key to ensure that digital retail can enter a new stage of development
- 第15章 mysql用户管理
- Excel VBA quick start (XII. Common usage of like comparison)
- 【工具】Apifox
- 软件测试技术之跨平台的移动端UI自动化测试(下)
- 【Qt多线程之线程的等待和唤醒】
猜你喜欢

matlab 短时自相关实现

JDBC summary

Let Xiaobai thoroughly understand performance tuning

Excel-vba quick start (X. prompt box, inputable pop-up box)

【工具】Apifox

Want the clouds in the picture to float? Video editing services can be achieved in three steps with one click

想让照片中的云飘起来?视频编辑服务一键动效3步就能实现

In depth analysis of the source code, why is the string class immutable? (hit me before you understand)
![[horizon sunrise X3 sect trial experience] + unpacking post](/img/b1/3196b470e601dc63ce3b11a7deb3cd.png)
[horizon sunrise X3 sect trial experience] + unpacking post

JS delay execution window.onload
随机推荐
Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering
Introduction to the notes of learning cesium by yourself
Understanding and practice of the trend of Bank of London foreign exchange
View绘制流程1-View与Window的关系
Xshell7 personal free download, use
When deploying Flink on a single machine and creating the connection table of oracle19c RAC, the error ora-12505 is reported. Who can help
AFNetworking了解
matlab 激励模型 三角波频谱
Iptables prevents nmap scanning and enables incremental backup of binlog
[RequireComponent(typeof(....))]
easyui的combobox默认选中第一个选项
ORM同时使用不同数据源和不同命名转换
[waiting and wakeup of QT multithreaded threads]
Let Xiaobai thoroughly understand performance tuning
In depth analysis of the source code, why is the string class immutable? (hit me before you understand)
C data type_ From rookie tutorial
Software Testing Technology: cross platform mobile UI automated testing (Part 2)
mysql推荐书
Triangular wave spectrum of MATLAB excitation model
Go----Go 语言中的标识符和关键字