力扣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;
      }
  };
相关推荐
Deng87234734842 分钟前
代码语法检查工具
linux·服务器·windows
风筝在晴天搁浅2 小时前
代码随想录 718.最长重复子数组
算法
kyle~2 小时前
算法---回溯算法
算法
star _chen2 小时前
C++实现完美洗牌算法
开发语言·c++·算法
hzxxxxxxx2 小时前
1234567
算法
Sylvia-girl3 小时前
数据结构之复杂度
数据结构·算法
CQ_YM3 小时前
数据结构之队列
c语言·数据结构·算法·
霍夫曼3 小时前
UTC时间与本地时间转换问题
java·linux·服务器·前端·javascript
VekiSon3 小时前
数据结构与算法——树和哈希表
数据结构·算法
月熊4 小时前
在root无法通过登录界面进去时,通过原本的普通用户qiujian如何把它修改为自己指定的用户名
linux·运维·服务器