力扣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;
      }
  };
相关推荐
语戚41 分钟前
力扣 1621. 大小为K的不重叠线段的数目:动态规划(Java 实现)
java·算法·leetcode·动态规划·力扣·dp
青山木1 小时前
Hot 100 --- 最长有效括号
java·数据结构·算法·leetcode·动态规划
光电笑映2 小时前
网络通信基础:从协议分层到 Socket 编程预备
linux·运维·服务器·网络
这料鬼有毒2 小时前
二刷hot100-1143.最长公共子序列
算法·leetcode·动态规划
政企项目老覃3 小时前
边缘 AI 推理部署:安防零售场景下的模型裁剪与端侧落地实践
人工智能·程序人生·算法·性能优化·vllm
mengge.cloud4 小时前
0917Docker 小白实验教程(理论+实操)
linux·服务器·网络·docker
GreenTea5 小时前
发布 3 天登顶 HN:不生成一个字的模型 Jev,我把它的源码和黑料都扒了一遍
前端·后端·算法
两点王爷5 小时前
常用的 Docker 镜像拉取地址仓库及常用命令详解
运维·服务器·容器
神.秘.人5 小时前
【BIOS/UEFI 与 MBR/GPT 的区别详解】
运维·服务器
第十人i5 小时前
Linux 一键重装系统脚本:reinstall 工具详解
linux·服务器