Python中列表按指定标准排序实例

概述


本题需要先输入机器的数目和任务的数目。

在接下来的n行中每行分别包含机器的最大执行时间和机器所能执行任务的最大强度。

在接下来的n行中每行分别包含任务执行时间和任务难度。任务收益为z = 200 * time + 3 * hard.

每台机器最多执行一个任务,求机器所能获得最大收益,以及机器所能执行的最大任务数量。

代码


nums = input().split(' ')
machine_nums = (int)(nums[0])
tasks_nums = (int)(nums[1]) machines = []
for i in range(machine_nums):
m_params = input().split(' ')
m_time = (int)(m_params[0])
m_hard = (int)(m_params[1])
machines.append((m_time, m_hard)) machines.sort(key = lambda x: (x[0], x[1])) tasks = []
for i in range(tasks_nums):
t_params = input().split(' ')
t_time = (int)(t_params[0])
t_hard = (int)(t_params[1])
t_income = 200 * t_time + 3 * t_hard
tasks.append((t_time, t_hard, t_income)) tasks.sort(key = lambda x: (-x[2])) max_task = 0
max_income = 0
for task in tasks:
for machine in machines:
if task[0] <= machine[0] and task[1] <= machine[1]:
max_task = max_task + 1
max_income = max_income + task[2]
machines.remove(machine)
break print(max_task, max_income)

Python中排序的灵活使用的更多相关文章

  1. Python中排序方法sort、函数sorted的key参数的作用分析

    从Python2.4开始,list.sort方法 和 sorted方法 都增加了一个 'key' 参数用来在进行比较之前指定每个列表元素上要调用的函数,将函数的返回值作为比较的依据. 那么怎么使用这个 ...

  2. Python中sort、sorted的cmp参数废弃之后使用cmp_to_key实现类似功能

    Python2.1以前的排序比较方法只提供一个cmp比较函数参数,没有__lt__等6个富比较方法, Python 2.1引入了富比较方法,Python3.4之后作废了cmp参数.相应地从Python ...

  3. Python中的字典排序

    我想将 b = {'a':234,'b':1,'c':2,'e':2387} 分别按照key和value进行排序,该怎样办呢? Python中比较常用的排序有两个函数, 一.定义 (1)一个是List ...

  4. 使用Python在2M内存中排序一百万个32位整数

    译言网 | 使用Python在2M内存中排序一百万个32位整数 使用Python在2M内存中排序一百万个32位整数 译者:小鼠 发表时间:2008-11-13浏览量:6757评论数:2挑错数:0 作者 ...

  5. Python中使用operator模块实现对象的多级排序

    Python中使用operator模块实现对象的多级排序 今天碰到一个小的排序问题,需要按嵌套对象的多个属性来排序,于是发现了Python里的operator模块和sorted函数组合可以实现这个功能 ...

  6. python中的list按照某一列进行排序的方法

    如题,python中的list着实很好用,我有如下一个list 可以看出list中的每一个元素是由字符串,两个新的list,以及一个float组成,现在想根据这最后一个float对这个list进行排序 ...

  7. Python中的排序方法sort(),sorted(),argsort()等

    python 列表排序方法sort.sorted技巧篇 转自https://www.cnblogs.com/whaben/p/6495702.html,学习参考. Python list内置sort( ...

  8. python中字典排序,列表中的字典排序

    python中字典排序,列表中的字典排序 一.使用python模块:operator import operator #首先要导入模块operator x = {1:2, 3:4, 4:3, 2:1, ...

  9. Python中对列表排序实例

    Python中对列表排序实例 发布时间:2015-01-04 09:01:50 投稿:junjie 这篇文章主要介绍了Python中对列表排序实例,本文给出了9个List的排序实例,需要的朋友可以参考 ...

随机推荐

  1. MyBatis使用懒加载mybatis-config.xml配置

    在mybatis-config.xml添加如下配置 <settings> <!--要使延迟加载生效必须配置下面两个属性--> <setting name="la ...

  2. 在git远程仓创建项目之后,提交本地项目的使用方法

    命令介绍 git 用户配置 git config --global user.name "张三" git config --global user.email "zhag ...

  3. Load average in Linux的精确含义

    Man 上的解释: load average System load averages is the average number of processes that are either in a ...

  4. myeclipse 安装svn(subeclipsesite)插件

    (1)到官网下载subeclipsesite,下载最新的版本:http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=224 ...

  5. 原生JS--各种兼容性写法总结

    <script> var oEvent = evt || event; ========================================================== ...

  6. 因JQUERY版本而产生的问题,需要加上迁移文件

    IMG_01_history控制台报错 IMG_02_history代码报错

  7. df - 报告文件系统磁盘空间的使用情况

    总览 df [OPTION]... [FILE]... POSIX 选项: [-kP] GNU 选项 (最短方式): [-ahHiklmPv] [-t fstype] [-x fstype] [--b ...

  8. docker 应用数据的管理之bind mounts

    创建容器使用bind mounts 挂载文件系统.宿主机文件系统会覆盖掉容器里初始数据 [root@localhost ~]# mkdir /www/htpm -pv mkdir: 已创建目录 &qu ...

  9. 背包DP || Codeforces 544C Writing Code

    程序员写bug的故事23333 题意:n个程序员,一共写m行程序,最多产生b个bug,问方案数 思路:f[i][j]表示写了i行,产生了j个bug的方案数,因为每个人都是可以独立的,所以i循环到n都做 ...

  10. 搜索 || DFS || POJ 1321 棋盘问题

    棋盘上#可以放,.不可以放,每行每列只能放一个 *解法:类似八皇后问题 dfs+回溯,考虑每一行和每一列 [[[[dfs的样子]]]]最前面写达到目标状态or不能走下去了 然后return #incl ...