155. 内存分配
https://www.acwing.com/problem/content/157/
#include <iostream> #include <algorithm> #include <queue> #include <set> #include <vector> using namespace std; typedef pair<int, int> PII; int n; queue<PII> q; // <len,duration> set<PII> memory; // <start, len> priority_queue<PII, vector<PII>, greater<PII>> heap; // <release time, start> int now, cnt; void init(int n) { memory.insert({-1, 1}); memory.insert({n, 1}); } bool allocate(int t, int m, int p) { for (auto it = memory.begin(); it != memory.end(); it ++ ) { auto jt = it; jt ++ ; if (jt != memory.end()) { int start = it->first + it->second; if (m <= jt->first - 1 - start + 1) { memory.insert({start, m}); heap.push({t + p, start}); return true; } } } return false; } void release(int t) { while (heap.size() && heap.top().first <= t) { int cur = heap.top().first; while (heap.size() && heap.top().first == cur) { auto top = heap.top(); heap.pop(); auto it = memory.lower_bound({top.second, 0}); memory.erase(it); } now = cur; while (q.size()) { auto front = q.front(); if (allocate(cur, front.first, front.second)) { q.pop(); } else break; } } } int main() { cin >> n; int t, m, p; init(n); while (cin >> t >> m >> p, t || m || p) { release(t); if (!allocate(t, m, p)) { q.push({m, p}); cnt ++ ; } } release(2e9); cout << now << endl << cnt << endl; return 0; }