Official

E - プロジェクト配属 / Project Assignment Editorial by admin Editorial for printing

Claude 4.6 Opus (Thinking)

Overview

This is a problem where \(N\) employees are assigned to \(M\) projects in order of priority. For each project, employees are assigned in descending order of skill points up to the required number, and we need to find the total skill points assigned to each project.

Analysis

Key Insight: The Assignment Structure is Simple

We process projects in descending order of priority, and for each project, the rule is “select \(R_j\) people with the highest skill points from those not yet assigned.”

There is an important observation here. If we sort the employees’ skill points in descending order, the assignment always consumes from the front of the sorted array in sequence.

Specifically: - The first project (highest priority) gets the top \(R_{j_1}\) employees by skill points - The next project gets the next \(R_{j_2}\) employees - And so on

This is because at each step we “choose from the remaining people with the highest skill points,” which is equivalent to slicing the sorted array from the front into consecutive blocks.

Comparison with a Naive Approach

An implementation that searches for and removes the top \(R_j\) employees from the remaining pool each time (e.g., re-sorting every time) would take \(O(NM)\) or more and could result in TLE. However, with the above observation, if we sort in advance, the problem reduces to range queries on a prefix sum, which can be solved very efficiently.

Algorithm

  1. Sort the employees’ skill points in descending order.
  2. Compute the prefix sum. \(\text{prefix}[k] = S[0] + S[1] + \cdots + S[k-1]\) (after sorting in descending order).
  3. Sort the projects in descending order of priority (and in ascending order of project number for ties).
  4. Maintain a variable \(\text{used} = 0\) (the number of employees assigned so far), and process projects in order:
    • If \(\text{used} + R_j > N\), the assignment fails — output \(-1\) and terminate.
    • Otherwise, the total skill points assigned to that project is \(\text{prefix}[\text{used} + R_j] - \text{prefix}[\text{used}]\).
    • Increase \(\text{used}\) by \(R_j\).
  5. If all projects are processed successfully, output the results in the original project number order.

Concrete Example

\(N=5\), skills \(= [3, 1, 4, 1, 5]\), with 2 projects: \((P_1=2, R_1=2), (P_2=5, R_2=1)\):

  • Skills sorted in descending order: \([5, 4, 3, 1, 1]\), prefix sum: \([0, 5, 9, 12, 13, 14]\)
  • Priority order: Project 2 (\(P=5\)) → Project 1 (\(P=2\))
  • Project 2: \(\text{used}=0\), top 1 person → total \(= 5 - 0 = 5\), \(\text{used}=1\)
  • Project 1: \(\text{used}=1\), next 2 people → total \(= 9 - 5 = 7\) (\(\text{prefix}[3]-\text{prefix}[1]\)), \(\text{used}=3\)
  • Output: Project 1 → \(7\), Project 2 → \(5\)

Complexity

  • Time complexity: \(O(N \log N + M \log M)\) (dominated by sorting; the assignment processing itself is \(O(M)\))
  • Space complexity: \(O(N + M)\) (for the prefix sum array and result array)

Implementation Notes

  • The sort key for projects can be (-P_j, j) to achieve descending order by priority and ascending order by project number for ties.

  • Since results must be output in the original project number order, store them indexed by the original number, such as results[j].

  • By precomputing the prefix sum, the total assignment for each project can be computed in \(O(1)\).

  • Include a check to detect the case \(R_1 + R_2 + \cdots + R_M > N\) during processing.

    Source Code

import sys
input = sys.stdin.readline

def main():
    N, M = map(int, input().split())
    S = list(map(int, input().split()))
    
    projects = []
    for j in range(M):
        P, R = map(int, input().split())
        projects.append((P, R, j))
    
    # Sort projects by priority descending, then by index ascending
    order = sorted(range(M), key=lambda j: (-projects[j][0], projects[j][2]))
    
    # Sort skills descending
    S.sort(reverse=True)
    
    # Compute prefix sums of sorted skills
    prefix = [0] * (N + 1)
    for i in range(N):
        prefix[i + 1] = prefix[i] + S[i]
    
    # Process projects in order, assigning top R_j available employees each time
    results = [0] * M
    used = 0  # number of employees already assigned
    
    for idx in order:
        P, R, j = projects[idx]
        if used + R > N:
            print(-1)
            return
        # Assign employees from index 'used' to 'used + R - 1' in the sorted array
        results[j] = prefix[used + R] - prefix[used]
        used += R
    
    out = []
    for j in range(M):
        out.append(str(results[j]))
    print('\n'.join(out))

main()

This editorial was generated by claude4.6opus-thinking.

posted:
last update: