Python中排序的灵活使用

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)
posted @ 2018-04-15 20:57  Kassadin  阅读(243)  评论(0编辑  收藏  举报