D - プロジェクトの締め切り / Project Deadline Editorial by admin
GPT 5.2 HighOverview
Consider each project as a “task that requires completing \(D_i\) units of work before its deadline,” and find the maximum number of projects that can be selected without exceeding the deadline (total available work time).
The solution uses a greedy approach: “if the total work of selected projects exceeds the deadline, discard the heaviest project.”
Analysis
Key Observation 1: This problem reduces to “Task Selection (Maximum Count)”
Project \(i\) with deadline on day \(i\) requires at least \(D_i\) total hours of work completed during days \(1\) through \((i-1)\).
Since at most \(H\) hours can be worked per day, the total available work time up to day \((i-1)\) is:
- 0 days: \(0\)
- 1 day: \(H\)
- …
- \((i-1)\) days: \((i-1)H\)
In other words, the deadline for project \(i\) can be thought of as “a total work time limit of \((i-1)H\)”.
Also, since work can be “split into non-negative integer hours and distributed across multiple projects,” the specific allocation details are not essential.
A chosen set of projects is feasible if, for every \(i\), the total required work for all selected projects with deadline \(\leq i\) is at most \((i-1)H\) (since work is divisible, what matters is “whether the cumulative total is sufficient” rather than the day-by-day ordering).
Key Observation 2: It suffices to process in deadline order (= chronological order)
Deadlines arrive in order \(1, 2, \dots, N\) (already sorted).
Therefore, it is sufficient to process from \(i=1\) onwards, maintaining whether the currently selected projects satisfy the deadline constraints.
Why a naive approach is difficult
- Optimizing over which subset to deliver is \(2^N\), which is infeasible.
- If we use DP with “time” as a state, the deadline can be up to \((N-1)H\) and \(H\) can be large (up to \(10^9\)), making the state space enormous and infeasible.
How to solve it: When exceeded, discard the “heaviest” one
Consider the set of projects selected up to day \(i\). If the total work \(S\) exceeds the deadline \((i-1)H\), it is infeasible.
In this case, to make it feasible again while reducing the project count as little as possible (= removing only 1 project), discarding the project with the maximum work is optimal.
Reason: - If we discard one project, removing the one that reduces \(S\) the most (the largest \(D\)) is the most advantageous for future deadlines as well. - By doing this each time, we maintain the state where “for the same number of projects, the total work is minimized” at every point.
For example, when \(H=5\) and \(i=4\), the deadline is \((4-1)H=15\).
If we have selected \(D=\{8,7,3\}\) with total \(18\), it exceeds the deadline, so discarding the largest (\(8\)) brings the total to \(10\), giving the greatest recovery.
Note that project 1 has a deadline of \(0\) (since \((1-1)H\)), so it is always discarded (consistent with the problem statement that “project 1 cannot be delivered”).
Algorithm
- Prepare a max-heap (in Python, use a min-heap with negated values) and a running total
total. - For \(i=1\) to \(N\), tentatively select project \(i\) (with work \(D_i\)):
total += D_i- Add \(D_i\) to the heap
- The deadline at this point (total available work time so far) is \(deadline = (i-1)H\).
Iftotal > deadline, the current set is infeasible, so:- Remove the maximum \(D\) from the heap (= reject the heaviest project)
- Subtract it from
totalas well
- The number of elements remaining in the heap at the end is the maximum number of deliverable projects.
This method is the same as the classic problem “maximum number of tasks with deadlines, processed greedily”: since deadlines are monotonically increasing, this greedy approach is optimal.
Complexity
- Time complexity: \(O(N \log N)\) (at most one heap push/pop per project)
- Space complexity: \(O(N)\) (the heap contains at most \(N\) elements)
Implementation Notes
Python’s
heapqis a min-heap, so to extract the maximum, we store \(-D_i\) in the heap.deadlineis \((i-1)H\). Usingenumerate(D, start=1)for 1-indexediallows writing the formula directly.The expression
total += heapq.heappop(heap)works because the heap stores negated values (e.g., popping \(-10\) and adding it tototalis equivalent to subtracting \(10\)).Source Code
import sys
import heapq
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
if not data:
return
N, H = data[0], data[1]
D = data[2:]
heap = [] # max-heap via negatives
total = 0
for i, d in enumerate(D, start=1):
deadline = (i - 1) * H
total += d
heapq.heappush(heap, -d)
if total > deadline:
total += heapq.heappop(heap) # subtract largest (negative)
print(len(heap))
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.