当前位置:网站首页>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
总结
两个量的问题不好求
先固定一个,然后再求另一个
一般来说,先固定简单的那个
边栏推荐
- Introduction to the notes of learning cesium by yourself
- ORM uses different data sources and different naming transformations at the same time
- 45、实例分割的labelme数据集转coco数据集以及coco数据集转labelme数据集
- Altium designer 22 Chinese character garbled
- matlab 画短时能量图
- [horizon sunrise X3 sect trial experience] + unpacking post
- Let me show you the MySQL isolation level. What happens when two transactions operate on the same row of data at the same time?
- 08 du 命令
- 09 expr 命令
- 实际权威来自信息优势
猜你喜欢

Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering

SQL injection less26 (filter spaces and comments, and use error injection without spaces)

Try new functions | decrypt Doris complex data type array

matlab 画短时能量图

Unity installation failed: operation not allowed, MKDIR

Join method in JS

matlab 画短时平均幅度谱

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

Xshell7 personal free download, use

Basic operation of (C language) files
随机推荐
Iptables prevents nmap scanning and enables incremental backup of binlog
Ansible installation and use
Go----Go 语言命名规范
Pytoch -- used by visdom
Let Xiaobai thoroughly understand performance tuning
SQL注入 Less24(二次注入)
unity 获取网络时间
ORM同时使用不同数据源和不同命名转换
win10下mysql改密码后,突然登录不上的问题解决过程记录
开发转测试:从零开始的6年自动化之路
JDBC summary
Instructions for use of light source controller dial switch
45. Instance segmented labelme dataset to coco dataset and coco dataset to labelme dataset
08 du 命令
matlab 画短时能量图
mysql推荐书
2018 arXiv preprint | MolGAN: An implicit generative model for small molecular graphs
Xshell7 personal free download, use
In depth analysis of the source code, why is the string class immutable? (hit me before you understand)
【Try to Hack】防火墙(一)