力扣1882.使用服务器处理任务

力扣1882.使用服务器处理任务

  • 两个优先队列

    • 一个放未使用的服务器
    • 一个放已使用的服务器
cpp 复制代码
  class Solution {
      using PLI = pair<long long, int>;
      using PII = pair<int, int>;
  public:
      vector<int> assignTasks(vector<int>& servers, vector<int>& tasks) {
          int m = servers.size(),n = tasks.size();
          priority_queue<PLI, vector<PLI>, greater<PLI>> busy;
          priority_queue<PII, vector<PII>, greater<PII>> idle;
          for(int i=0;i<m;i++)
              idle.emplace(servers[i],i);
          
          long long ts = 0;
          //把占用结束的服务器弹出
          auto release = [&]()
          {
              while(!busy.empty() && busy.top().first <= ts)
              {
                  auto &&[_,idx] = busy.top();
                  idle.emplace(servers[idx],idx);
                  busy.pop();
              }
          };
  
          vector<int> res;
          for(int i=0;i<n;i++)
          {
              ts = max(ts, (long long)i);
              //把占用结束的弹出
              release();
              if(idle.empty())
              {
                  ts = busy.top().first;
                  release();
              }
              //取未占用的服务器
              auto&& [_,idx] = idle.top();
              res.push_back(idx);
              busy.emplace(ts+tasks[i] , idx);
              idle.pop();
          }
          return res;
      }
  };
相关推荐
前进的李工25 分钟前
LeetCode hot100:234 回文链表:快慢指针巧判回文链表
python·算法·leetcode·链表·快慢指针·回文链表
sin_hielo30 分钟前
leetcode 3228
算法·leetcode
CS_浮鱼1 小时前
【Linux】进程概念
linux·运维·服务器
xier_ran1 小时前
力扣(LeetCode)100题:41.缺失的第一个正数
数据结构·算法·leetcode
Swift社区2 小时前
LeetCode 425 - 单词方块
算法·leetcode·职场和发展
青柚~2 小时前
【鲲鹏服务器麒麟系统arm架构部署docker】
服务器·arm开发·docker·架构
人工智能训练2 小时前
Ubuntu中如何进入root用户
linux·运维·服务器·人工智能·ubuntu·ai编程·root
tianshiyeben2 小时前
WGCLOUD监控系统使用指南 - 告警消息整理完整版
linux·运维·服务器·系统安全·zabbix
Unlyrical2 小时前
splice, io_uring_prep_splice 调用(无效参数)
linux·服务器·c++·unix
牛奶咖啡132 小时前
银河麒麟高级服务器系统(V11)的安装部署实操保姆级教程
服务器·银河麒麟高级服务器操作系统安装·银河麒麟高级服务器v11配置·设置基础软件仓库时出错·银河麒高级服务器系统的实操教程·生产级部署银河麒麟服务系统教程·linux系统的快速上手教程