题目:3492. 负载均衡
思路:用到优先队列,队列里面存的是pair,第一个是用完的时间点,第二个是需要消耗的计算机能力,然后载每一次任务之前,都遍历一遍对应的计算机的队列,把已经可以执行完的任务的计算机能力回收,最后来判断是否可以分配当前任务
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2e5+10;
typedef pair<int ,int >PII;
priority_queue<PII,vector<PII>,greater<PII>>q[N];
int n,m;
int nl[N];
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)
scanf("%d",&nl[i]);
for(int i=0;i<m;i++){
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
while(q[b].size()&&q[b].top().first<=a){
nl[b]+=q[b].top().second;
q[b].pop();
}
if(nl[b]<d) puts("-1");
else{
q[b].push({a+c,d});
nl[b]-=d;
printf("%d\n",nl[b]);
}
}
return 0;
}