zoukankan      html  css  js  c++  java
  • 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)
    
  • 相关阅读:
    Apple Tree(树状数组+线段树)
    平衡阵容(RMQ st表算法)
    一起去打CS
    夜归

    淋雨
    大马戏
    雨中行船
    弄堂里
    夜游天安门
  • 原文地址:https://www.cnblogs.com/Kassadin/p/8849427.html
Copyright © 2011-2022 走看看