D - プロジェクトの締め切り / Project Deadline Editorial by admin Editorial for printing
gemini-3.5-flash-thinkingOverview
There are \(N\) projects, each with a given deadline and required workload. Given that you can work at most \(H\) hours per day, the problem asks for the maximum number of projects that can be delivered on time.
Analysis
1. Reformulating the Problem
To complete project \(i\) (with a deadline on day \(i\)), work must be done before the deadline, specifically during the \(i-1\) days from day \(1\) to day \(i-1\). Therefore, the maximum effective time available to finish project \(i\) is \((i-1) \times H\) hours.
From this, the problem can be reformulated as follows: * At the end of day \(k\) (\(1 \leq k \leq N-1\)), the total workload of all projects decided to be “delivered” so far must not exceed the maximum available time of \(k \times H\) hours.
Note that project \(1\) (with a deadline on day \(1\)) cannot be delivered under any circumstances since there is no preceding day to work on it. Therefore, we only need to consider the \(N-1\) projects from project \(2\) to project \(N\).
2. Applying a Greedy Approach
We examine projects in order of earliest deadline (day \(2\), day \(3\), \(\dots\), day \(N\)).
If, after processing work up to day \(i\), the total workload of selected projects exceeds \(i \times H\), we need to give up (not deliver) one of the previously selected projects. In this case, which project should we give up?
Naturally, giving up the “project with the largest workload \(D\) among those selected so far” is optimal. The number of projects given up is the same (one), but since this maximally reduces the total workload, it becomes easier to satisfy subsequent deadline constraints (such as \((i+1) \times H\)).
This approach of “when the condition is violated, discard the heaviest item from the current selection” (greedy method) works correctly.
3. Choosing the Data Structure
To efficiently “extract the maximum value from the currently selected projects,” we use a priority queue (max-heap). This allows fast insertion of elements and deletion of the maximum value.
Algorithm
We use 0-indexed notation (indices from \(0\) to \(N-1\)). We process project \(i\) (deadline on day \(i+1\), can work up to day \(i\)) in order.
- Initialize a priority queue
pq(with the maximum value at the front) and the total workload of currently selected projectssum_Dto \(0\). - Repeat the following for \(i = 1\) to \(N-1\):
- Add
D[i]topqand addD[i]tosum_D. - While
sum_Dexceeds \(i \times H\), repeat the following:- Extract the maximum value (the project with the largest workload) from
pq. - Subtract that value from
sum_Dand remove it frompq.
- Extract the maximum value (the project with the largest workload) from
- Add
- The number of elements remaining in
pqat the end is the maximum number of projects that can be delivered on time.
Complexity
Time Complexity: \(O(N \log N)\) Each project is added to the priority queue at most once (
push) and removed at most once (pop). Each priority queue operation on up to \(N\) elements takes \(O(\log N)\), so the overall time complexity is \(O(N \log N)\), which is well within the time limit.Space Complexity: \(O(N)\) The input array
Dand the priority queuepqstore at most \(N\) elements, so the space complexity is \(O(N)\).
Implementation Notes
Preventing Overflow: Since \(N \le 2 \times 10^5\) and \(H \le 10^9\), the maximum available time \(i \times H\) can reach approximately \(2 \times 10^{14}\). Similarly, the total workload
sum_Dcan become equally large. These values exceed the range of 32-bit integers, so in C++ you must use thelong longtype.Index Offset: The code uses 0-indexed processing. Project \(i\) (
D[i]) has a deadline on day \(i+1\), and work can be done up to day \(i\) (maximum \(i \times H\) hours). Note that the time limit formula in the code becomes(long long)i * H.Source Code
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main() {
// Optimize standard I/O operations for competitive programming
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
long long H;
if (!(cin >> N >> H)) return 0;
vector<long long> D(N);
for (int i = 0; i < N; ++i) {
cin >> D[i];
}
priority_queue<long long> pq;
long long sum_D = 0;
// We consider projects from index 1 to N-1 (0-indexed).
// For project i (which is due on day i+1), the work must be done within the first i days.
// Thus, the total available work time up to this point is i * H.
for (int i = 1; i < N; ++i) {
pq.push(D[i]);
sum_D += D[i];
long long limit = (long long)i * H;
// If the current total workload exceeds the available time,
// greedily discard the project with the largest workload.
while (sum_D > limit) {
sum_D -= pq.top();
pq.pop();
}
}
cout << pq.size() << "\n";
return 0;
}
This editorial was generated by gemini-3.5-flash-thinking.
posted:
last update: