Official

D - プロジェクトの締め切り / Project Deadline Editorial by admin Editorial for printing

gemini-3.5-flash-thinking

Overview

This problem asks us to determine the maximum number of projects that can be delivered on time, given multiple projects with deadlines. Given the maximum working hours per day, and the deadline and required work amount for each project, we efficiently find the maximum number of schedulable projects using “Regret Greedy”.

Analysis

1. Organizing the Basic Rules

  • Project \(i\), which has its deadline on day \(i\), must be completed by **working from day \(1\) to day \(i-1\).
  • Therefore, Project \(1\) (deadline on day \(1\)) can never be delivered, since there is no previous day available for work.
  • By the end of day \(i\) (1-indexed), the maximum cumulative working time Takahashi can secure is \(i \times H\) hours.

2. Condition for a Schedule to be Feasible

Suppose we select some projects from Projects \(2\) through \(i+1\) for delivery. The necessary and sufficient condition for all selected projects to actually be delivered within their deadlines is that the following condition holds for any \(k\) (\(1 \le k < N\)):

\[\text{(Sum of work amounts among selected projects with deadlines on or before day } k+1\text{)} \le k \times H\]

When examining projects in order of earliest deadline, we want to maximize the number of selected projects while always satisfying this condition.

3. Greedy Approach (Regret Greedy)

We select projects in order of earliest deadline (Projects \(2, 3, \ldots, N\)) using the following strategy:

  1. Tentatively assume we will deliver the new project \(i+1\) (with work amount \(D_{i+1}\)) and add it to the selection list.
  2. If the total work amount of all selected projects exceeds the current cumulative available time \(i \times H\) (i.e., the schedule becomes infeasible):
    • Among the projects we decided to “deliver,” give up the one with the largest work amount (the most time-consuming project).
    • By giving up the heaviest project, we reduce the total work amount by the largest possible amount while only decreasing the project count by \(1\). This creates room to select more projects in the future.

This approach of “tentatively selecting, then canceling (regretting) the most inefficient past choice when the limit is exceeded” is called Regret Greedy, and it is guaranteed to produce the optimal solution for this problem.

Simulation with a Concrete Example

Consider \(H = 5\) and project work amounts \([D_1, D_2, D_3, D_4] = [10, 4, 8, 2]\). * Project 1 (\(D_1=10\)) is excluded from the start. * \(i=1\) (end of day 1, limit \(1 \times 5 = 5\) hours): * Add Project 2 (\(D_2=4\)). Selected list: \(\{4\}\) (sum \(4\)). * Sum \(4 \le 5\), so it’s safe. * \(i=2\) (end of day 2, limit \(2 \times 5 = 10\) hours): * Add Project 3 (\(D_3=8\)). Selected list: \(\{4, 8\}\) (sum \(12\)). * Sum \(12 > 10\), so it overflows. Give up the maximum \(8\) from the list. * Selected list returns to: \(\{4\}\) (sum \(4\)). * \(i=3\) (end of day 3, limit \(3 \times 5 = 15\) hours): * Add Project 4 (\(D_4=2\)). Selected list: \(\{4, 2\}\) (sum \(6\)). * Sum \(6 \le 15\), so it’s safe.

The final selected list is \(\{4, 2\}\) with \(2\) projects, which is the maximum.


Algorithm

To efficiently retrieve and remove the “maximum value (the project with the largest work amount)” from the selected projects, we use a priority queue (max-heap).

  1. Prepare an empty max-heap pq and a variable current_sum = 0 representing the total work amount of selected projects.
  2. Loop \(i\) from \(1\) to \(N-1\) (processing project \(i+1\) with work amount \(D_i\) in 0-indexed order).
    • Add \(D_i\) to pq and add \(D_i\) to current_sum.
    • Calculate the current time limit limit = i * H.
    • If current_sum > limit, pop the maximum value from pq and subtract that value from current_sum.
  3. After the loop ends, the number of elements remaining in pq is the maximum number of projects that can be delivered.

Complexity

  • Time Complexity: \(O(N \log N)\)
    • The loop executes \(N-1\) times.
    • Insertion into and extraction of the maximum from the priority queue (heap) within each loop iteration takes \(O(\log K)\) where \(K \le N\) is the number of elements.
    • Therefore, the overall complexity is \(O(N \log N)\), which runs sufficiently fast for the constraint \(N \le 2 \times 10^5\) (well within the time limit).
  • Space Complexity: \(O(N)\)
    • The priority queue stores at most \(N\) elements. Additionally, the array for holding input data uses \(O(N)\) memory.

Implementation Notes

  • Implementing a Max-Heap: Python’s heapq module provides a Min-Heap by default. Therefore, when inserting values into the heap, we negate them (store as \(-d\)) to make it function as a max-heap. When extracting a value (heappop), we multiply by minus again to restore the original positive number.

    Source Code

import sys
import heapq

def solve():
    input_data = sys.stdin.read().split()
    if not input_data:
        return
    N = int(input_data[0])
    H = int(input_data[1])
    D = [int(x) for x in input_data[2:]]
    
    pq = []
    current_sum = 0
    
    for i in range(1, N):
        d = D[i]
        heapq.heappush(pq, -d)
        current_sum += d
        limit = i * H
        if current_sum > limit:
            max_val = -heapq.heappop(pq)
            current_sum -= max_val
            
    print(len(pq))

if __name__ == '__main__':
    solve()

This editorial was generated by gemini-3.5-flash-thinking.

posted:
last update: