力扣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;
      }
  };
相关推荐
馨羽的玩具22 分钟前
查哪个程序一直登录sql server失败
运维·服务器·数据库
luoqice1 小时前
在嵌入式 Linux 系统中,配置 DNS 以实现内网或外网连接
linux·运维·服务器
妙妙屋(zy)1 小时前
基于Astro开发的Fuwari静态博客模版配置CICD流程
服务器·docker
IvanCodes1 小时前
三、Linux用户与权限管理详解
linux·运维·服务器
默默敲代码的徐哥儿1 小时前
八股文整理——计算机网络
服务器·网络·计算机网络
吃着火锅x唱着歌2 小时前
LeetCode 1616.分割两个字符串得到回文串
算法·leetcode·职场和发展
孟大本事要学习2 小时前
算法第28天|动态规划:基础理论、斐波那契数、爬楼梯、使用最小花费爬楼梯
算法·动态规划
LastWhisperw2 小时前
音频算法基础(语音识别 / 降噪 / 分离)
算法·音视频·语音识别
lingling0092 小时前
艾利特机器人:光伏机器人如何重塑清洁能源制造新格局
大数据·人工智能·算法
流星白龙2 小时前
【C++算法】75.优先级队列_数据流中的第 K 大元素
开发语言·c++·算法