D - プロジェクトの締め切り / Project Deadline Editorial by admin Editorial for printing
Claude 4.6 Opus (Thinking)Overview
Given \(N\) projects ordered by deadline, the problem asks to maximize the number of projects that can be delivered on time, subject to a constraint of at most \(H\) working hours per day. We solve this using a combination of a greedy approach and a max-heap.
Analysis
Key Observation: Feasibility Condition
Consider the projects sorted by deadline (in this problem, project \(i\) has its deadline on day \(i\), so the index order equals the deadline order).
When we select a subset of projects, we consider the condition for all of them to be completable within their deadlines. Let the selected projects in deadline order be \(p_1, p_2, \ldots, p_k\):
- All work for projects up to \(p_m\) (with deadline on day \(d_m\)) must be completed during days \(1\) through \((d_m - 1)\).
- The maximum available working time during this period is \((d_m - 1) \times H\) hours.
Therefore, if for all \(m\), \(D_{p_1} + D_{p_2} + \cdots + D_{p_m} \leq (d_m - 1) \times H\) holds, then all projects can be delivered with appropriate scheduling.
Problem with the Naive Approach
Trying all subsets would be \(O(2^N)\), which is far too slow for \(N \leq 2 \times 10^5\).
Solution: Greedy Approach
We process projects in order of earliest deadline, adopting the policy of “selecting as many as possible.” If adding a new project violates the constraint, we remove the project with the largest workload from the current selection, thereby recovering feasibility while maintaining at least the same count.
Why is this optimal? Removing the project with the largest workload maximally reduces the “total time used,” thereby maximizing the room to accept future projects.
Algorithm
- Process projects in order \(i = 1, 2, \ldots, N\) (= deadline order).
- Maintain a max-heap (managing the workloads of selected projects) and the total workload of selected projects
total_work. - Add project \(i\): push \(D_i\) onto the heap and set
total_work += D_i. - If
total_work > (i - 1) * H(constraint violation), pop the maximum value from the heap and exclude it. - The final number of elements remaining in the heap is the answer.
Concrete Example
For \(N = 4, H = 3, D = [1, 2, 4, 3]\): - \(i=1\): Available time \(= 0\). \(D_1 = 1 > 0\), so exclude it. Selection count \(= 0\). - \(i=2\): Available time \(= 3\). \(D_2 = 2 \leq 3\). Selection count \(= 1\). - \(i=3\): Available time \(= 6\). Total \(= 2 + 4 = 6 \leq 6\). Selection count \(= 2\). - \(i=4\): Available time \(= 9\). Total \(= 2 + 4 + 3 = 9 \leq 9\). Selection count \(= 3\).
Answer: \(3\)
Complexity
- Time complexity: \(O(N \log N)\) (heap insertion/deletion is \(O(\log N)\) for each project)
- Space complexity: \(O(N)\) (heap size)
Implementation Notes
Python’s
heapqis a min-heap, so we store values as negatives to use it as a max-heap.The
while total_work > available_timeloop actually executes at most once per project addition (adding one and removing one always satisfies the constraint, because the removed element has workload at least as large as the added one).Since \(D_i \geq 1\), project 1 (with deadline on day 1, available time 0) can never be delivered, which is consistent with the problem statement.
Since \(H\) and \(D_i\) can be up to \(10^9\), the total values can become large, but in Python there is no concern about integer overflow.
Source Code
import heapq
def solve():
import sys
input_data = sys.stdin.buffer.read().split()
N = int(input_data[0])
H = int(input_data[1])
D = [int(input_data[i + 2]) for i in range(N)]
# Project i has deadline on day i (1-indexed).
# To complete project i, we need D[i] hours of work on days 1..i-1.
# On day j, we can work at most H hours total across all projects.
# Work on day j can only go to projects with deadline >= j+1.
# Key insight: if we select a subset S of projects to complete,
# we need to schedule their work before their deadlines.
# Sort selected projects by deadline. For projects with deadlines d1 <= d2 <= ... <= dk,
# the total available time before deadline d_m is (d_m - 1) * H.
# We need: for each prefix of selected projects (sorted by deadline),
# sum of D values <= (deadline_of_last_in_prefix - 1) * H.
# Greedy approach: process projects in order of deadline (which is just i = 1..N).
# Maintain a set of selected projects. When adding project i, if the constraint
# is violated, remove the project with the largest D value.
# Use a max-heap.
max_heap = [] # stores negative D values for max-heap behavior
total_work = 0
count = 0
for i in range(N):
deadline = i + 1 # 1-indexed
d = D[i]
available_time = (deadline - 1) * H # hours available before day `deadline`
# Project 1 has deadline=1, available_time=0, can only be done if D[0]=0
# But D_i >= 1, so project 1 can never be done. That's consistent with problem statement.
# Try to add this project
heapq.heappush(max_heap, -d)
total_work += d
count += 1
# If total work exceeds available time, remove the largest D project
while total_work > available_time:
largest = -heapq.heappop(max_heap)
total_work -= largest
count -= 1
print(count)
solve()
This editorial was generated by claude4.6opus-thinking.
posted:
last update: