力扣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;
      }
  };
相关推荐
Erik_LinX12 分钟前
算法日记36:leetcode095最长公共子序列(线性DP)
算法
2301_7665360514 分钟前
刷leetcode hot100--动态规划3.11
算法·leetcode·动态规划
VT.馒头14 分钟前
【力扣】2629. 复合函数——函数组合
前端·javascript·算法·leetcode
DOMINICHZL25 分钟前
卡尔曼滤波算法从理论到实践:在STM32中的嵌入式实现
stm32·嵌入式硬件·算法
IT成长日记32 分钟前
Ansible运行原理揭秘:如何用YAML脚本掌控数服务器?
运维·服务器·ansible·架构与原理
CodeJourney.40 分钟前
光储直流微电网:能源转型的关键力量
数据库·人工智能·算法·能源
GUOYUGRA44 分钟前
高纯氢能源在线监测分析系统组成和作用
人工智能·算法·机器学习
猪猪侠|ZZXia1 小时前
# linux有哪些显示服务器协议、显示服务器、显示管理器、窗口管理器?有哪些用于开发图形用户界面的工具包?有哪些桌面环境?
linux·服务器
人间凡尔赛1 小时前
VSCode-Server 在 Linux 容器中的手动安装指南
linux·运维·服务器·docker
Chenyu_3101 小时前
05.基于 TCP 的远程计算器:从协议设计到高并发实现
linux·网络·c++·vscode·网络协议·tcp/ip·算法