Question Link
There are n tasks, and each task must execute W of workload within [R, D] (the three variables are integers ). The processing speed of the processor can change. When the speed is S, the time required for executing a task with a workload of W is w/s. In addition, it does not have to be executed consecutively and can be divided into several blocks. The minimum value of the maximum speed of the processor during execution. The processor speed is any integer.
Idea: in fact, it is similar to minimizing the maximum value, that is, finding the minimum speed when each task is completed within a given time range. We can first sort the start time from small to large, and then enumerate the time. the start time is smaller than the current enumeration time, and the earlier the task is completed, the higher the priority. Then decide which task or task the time processor processes. Note that when the end time of the first element in the queue is less than the current enumerated time, this task is not completed within the specified time, so the speed is not enough.
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int N = 10000000;const int MAXN = 10005;struct Work{ int r, d, w; friend bool operator < (Work a, Work b) { return a.d > b.d; }}work[MAXN];int n;int cmp(Work a, Work b) { return a.r < b.r;}int judge(int mid) { priority_queue<Work> q; Work state; int cnt = 0; for (int i = 1; i <= 20000; i++) { if (!q.empty()) { state = q.top(); if (state.d < i) { return false; } } while (cnt < n && work[cnt].r + 1 <= i) { q.push(work[cnt++]); } int sum = mid; while (sum && !q.empty()) { state = q.top(); q.pop(); if (sum < state.w) { state.w -= sum; sum = 0; q.push(state); } else sum -= state.w; if (cnt == n && q.empty()) return true; } } return false;}int main() { int cas; scanf("%d", &cas); while (cas--) { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d%d%d", &work[i].r, &work[i].d, &work[i].w); sort(work, work + n, cmp); int L = 0, R = N, mid; while (L < R) { mid = L + (R - L) / 2; if (judge(mid)) R = mid; else L = mid + 1; } printf("%d\n", L); } return 0;}