From 48e33d2718945bdcb5a2d1cdb26db58880a2a536 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 18 Jul 2024 11:28:56 +0800 Subject: [PATCH] =?UTF-8?q?P8755=20[=E8=93=9D=E6=A1=A5=E6=9D=AF=202021=20?= =?UTF-8?q?=E7=9C=81=20AB2]=20=E8=B4=9F=E8=BD=BD=E5=9D=87=E8=A1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/166829360 --- Luogu/P8755/P8755.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Luogu/P8755/P8755.cpp diff --git a/Luogu/P8755/P8755.cpp b/Luogu/P8755/P8755.cpp new file mode 100644 index 00000000..6011dce7 --- /dev/null +++ b/Luogu/P8755/P8755.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 2e5 + 5; + +int n, m, v[N]; +std::priority_queue< + std::pair, + std::vector>, + std::greater>> + q[N]; + +bool assign(int now_time, int id, int use_time, int use) { + // 移除已经完成的任务,释放资源。一定要判非空! + while (!q[id].empty() && q[id].top().first <= now_time) { + v[id] += q[id].top().second; + q[id].pop(); + } + + if (v[id] < use) { // 算力不足 + return false; // 分配失败 + } + + v[id] -= use; + q[id].emplace(now_time + use_time, use); + + return true; // 成功 +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + 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; + + if (assign(a, b, c, d)) { + cout << v[b] << endl; + } else { + cout << -1 << endl; + } + } + + return 0; +}