b711中等
等候增優的單台處理
每個候選在時刻 t 的優先值為 t+(pᵢ−aᵢ),共同的 t 不影響比較。因此先依到達時間排序,再用堆按 pᵢ−aᵢ 最大、aᵢ 最小、輸入編號最小選工作。
解題思路
每個候選在時刻 t 的優先值為 t+(pᵢ−aᵢ),共同的 t 不影響比較。因此先依到達時間排序,再用堆按 pᵢ−aᵢ 最大、aᵢ 最小、輸入編號最小選工作。
每次選取前把所有 aᵢ≤t 的工作加入堆;堆空就跳到下一個到達時間。取出後等待加 t−aᵢ,時間增加 dᵢ。此順序完全等價於題意逐次決策,尤其同完成時刻的新到達者會在下一次選取前加入,不會錯過競爭。
時間 O(n log n)、空間 O(n),包含 Python 排序列表、heap、分詞緩衝;n≤1700。完成與等候總和可超 32 位元。
Step-by-step animation
自訂小例:工作 (a,p,d) 為 (0,0,3)、(1,0,1)。
步驟 1 / 3
步驟 1:第一筆開始
工作 (a,p,d) 為 (0,0,3)、(1,0,1)。
工作1
- t
- 0
- 等待和
- 0
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n;
cin >> n;
vector<array<ll, 4>> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i][0] >> a[i][1] >> a[i][2];
a[i][3] = i;
}
sort(a.begin(), a.end());
priority_queue<array<ll, 4>, vector<array<ll, 4>>, greater<array<ll, 4>>> h;
int j = 0;
ll t = 0, w = 0;
while (j < n || !h.empty()) {
if (h.empty() && j < n)
t = max(t, a[j][0]);
while (j < n && a[j][0] <= t) {
auto [x, p, d, i] = a[j++];
h.push({x - p, x, i, d});
}
auto [k, x, i, d] = h.top();
h.pop();
w += t - x;
t += d;
}
cout << t << ' ' << w << '\n';
}
先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。