AcWing 3492. 负载均衡【优先队列+模拟】
原题链接
中等
作者:
晓龙coding
,
2022-02-13 13:45:05
,
所有人可见
,
阅读 980
/*
* author: longcoding
* contact: 208827435@qq.com
*/
#include <bits/stdc++.h>
#pragma GCC target ("avx")
#pragma GCC optimize (2, 3, "Ofast", "inline", "-ffast-math")
using namespace std;
const int N = 200010;
int v[N];
struct Node {
int ed, id, cnt;
bool operator>(const Node& n) const {
return ed > n.ed;
}
};
priority_queue<Node, vector<Node>, greater<Node> > q;
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> v[i];
}
while (m--) {
int a, b, c, d;
cin >> a >> b >> c >> d;
// 先回收电脑的内存
while (q.size() && q.top().ed <= a) {
Node t = q.top();
q.pop();
v[t.id] += t.cnt;
}
// 再分配任务
if (v[b] >= d) {
v[b] -= d;
q.push({ a + c, b, d });
cout << v[b] << endl;
}
else {
cout << -1 << endl;
}
}
return 0;
}