力扣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;
      }
  };
相关推荐
酉鬼女又兒3 分钟前
每天一个Linux命令_printf
linux·运维·服务器
翼龙云_cloud4 分钟前
国际云代理商:2026年国际云注册风控升级实战指南 8 大平台无卡解决方案对比
服务器·阿里云·云计算
Z9fish6 分钟前
sse哈工大C语言编程练习20
c语言·开发语言·算法
Trouvaille ~9 分钟前
TCP Socket编程实战(三):线程池优化与TCP编程最佳实践
linux·运维·服务器·网络·c++·网络协议·tcp/ip
晓131311 分钟前
第六章 【C语言篇:结构体&位运算】 结构体、位运算全面解析
c语言·算法
iAkuya17 分钟前
(leetcode)力扣100 61分割回文串(回溯,动归)
算法·leetcode·职场和发展
June`18 分钟前
高并发网络框架:Reactor模式深度解析
linux·服务器·c++
梵刹古音20 分钟前
【C语言】 指针与数据结构操作
c语言·数据结构·算法
VT.馒头26 分钟前
【力扣】2695. 包装数组
前端·javascript·算法·leetcode·职场和发展·typescript
WHD3061 小时前
苏州勒索病毒加密 服务器数据解密恢复
运维·服务器