← 返回题目页

T0571 物流运输

标程

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;
    vector<long long> cap(n);
    for (int i = 0; i < n; i++) cin >> cap[i];

    // 每个小组:正在进行的任务(按结束时间的小根堆)与当前被占用的运力
    using P = pair<long long, long long>;   // (结束时间, 占用运力)
    vector<priority_queue<P, vector<P>, greater<P>>> busy(n);
    vector<long long> used(n, 0);

    for (int i = 0; i < m; i++) {
        long long st, id, t, d;
        cin >> st >> id >> t >> d;
        id--;

        // 归还已经结束的任务所占用的运力(任务在 [st, st+t) 期间占用)
        while (!busy[id].empty() && busy[id].top().first <= st) {
            used[id] -= busy[id].top().second;
            busy[id].pop();
        }

        if (used[id] + d > cap[id]) {
            cout << -1 << '\n';          // 运力不足,取消这次任务
        } else {
            used[id] += d;
            busy[id].push({st + t, d});
            cout << cap[id] - used[id] << '\n';
        }
    }
    return 0;
}

动画演示