解題思路
分別計算三種硬體資源的最大同時用量。
週期長P=24D,將每筆起始日與時換成分鐘軸上的整點小時t。持續h小時是半開區間[t,t+h)。
若超過P就拆成[t,P)與[0,t+h−P),每段起點加三種資源、終點扣回;h=0不產生占用。
將所有事件按時間排序,同一時刻合併後更新當前用量,各資源獨立維護最大值。不須配置D大小的時間表,所以D無上界仍可處理。
每群s筆,時間 O(s log s)次精確時間比較、工作空間 O(s)。Python一次split另保留全部群的 O(Σs)筆輸入;大整數位數成本另計。
Step-by-step animation
D=1、23時起持續2小時的跨週期示例
步驟 1 / 3
步驟 1
示例D=1,23時啟動持續2小時。
[23,25)
- 步驟
- 1
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using boost::multiprecision::cpp_int;
using ll = long long;
int main() {
int groups;
cpp_int D;
cin >> groups >> D;
cpp_int P = D * 24;
while (groups--) {
int s;
cin >> s;
map<cpp_int, array<ll, 3>> events;
auto put = [&](cpp_int l, cpp_int r, array<ll, 3> v) {
if (l >= r)
return;
for (int j = 0; j < 3; j++) {
events[l][j] += v[j];
events[r][j] -= v[j];
}
};
for (int i = 0; i < s; i++) {
cpp_int day, h;
int hour;
array<ll, 3> v;
cin >> day >> hour >> h >> v[0] >> v[1] >> v[2];
cpp_int l = (day - 1) * 24 + hour, r = l + h;
if (r <= P)
put(l, r, v);
else {
put(l, P, v);
put(0, r - P, v);
}
}
int zero;
cin >> zero;
array<ll, 3> cur{}, ans{};
for (auto [t, v] : events)
for (int j = 0; j < 3; j++) {
cur[j] += v[j];
ans[j] = max(ans[j], cur[j]);
}
cout << ans[0] << ' ' << ans[1] << ' ' << ans[2] << '\n';
}
}
先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。