E - プロジェクト配属 / Project Assignment Editorial by admin
Qwen3-Coder-480BOverview
Given employees’ skill points and projects’ priorities and required number of people, determine whether employees can be assigned to projects according to priority order, and if possible, output the total skill of employees assigned to each project.
Analysis
In this problem, projects must be processed in descending order of priority (and in ascending order of project number for ties). For each project, we must assign the specified number of unassigned employees, selecting them in descending order of skill.
A naive approach of maintaining “unassigned employees,” sorting them each time, and taking from the front would cost \(O(NM)\) in the worst case, resulting in TLE.
The key insight is that “once an employee is selected, they cannot be reused” and “employees are selected in descending order of skill,” so if we sort all employees by skill in descending order at the beginning, we can simply use them sequentially from the front. This eliminates the need to sort every time, making the solution efficient.
Furthermore, since the processing order of projects is determined, we just need to manage “how many top employees to use among those currently available” according to that order. While data structures like heaps could be considered for “efficiently retrieving the top \(k\) people,” in this case we only need to use a pre-sorted list from the front, so it can be managed with a pointer.
Algorithm
- Sort employees’ skills \(S\) in descending order.
- Sort projects in descending order of priority, and in ascending order of project number for ties.
- Prepare a pointer
ptrto manage “the front position of employees not yet used.” - For each project:
- If the remaining number of employees is insufficient for the required number \(R\), output
-1. - If sufficient, sum the skills of \(R\) employees starting from
ptr, and advanceptraccordingly. - Store the result associated with the original project number.
- If the remaining number of employees is insufficient for the required number \(R\), output
- If all projects succeed, output the results in order.
Complexity
- Time complexity: \(O(N \log N + M \log M)\)
- Dominated by sorting (employee sort \(O(N \log N)\), project sort \(O(M \log M)\))
- Space complexity: \(O(N + M)\)
- Used for storing the skill list and project list
Implementation Notes
By sorting skill points in descending order at the beginning and managing the “range of available employees” with a pointer, we achieve speedup without sorting each time.
Projects are sorted together with their priority and original index.
Results are stored using the original index rather than the post-sort order, to correctly associate them with the original project numbers.
Source Code
import heapq
from collections import defaultdict
import sys
input = sys.stdin.read
def main():
import sys
input = sys.stdin.read
data = input().split()
idx = 0
N = int(data[idx]); idx += 1
M = int(data[idx]); idx += 1
S = [0]*N
for i in range(N):
S[i] = int(data[idx]); idx += 1
projects = []
for j in range(M):
P = int(data[idx]); idx += 1
R = int(data[idx]); idx += 1
projects.append((P, R, j))
# 優先度降順、同じなら番号昇順
projects.sort(key=lambda x: (-x[0], x[2]))
# スキルポイントを降順ソートしたリスト(使用可能社員)
S.sort(reverse=True)
available = S[:]
# 結果を保存する配列(インデックス -> 合計スキル)
result = [0]*M
# 現在選べる社員のインデックス(availableの先頭からR個選ぶ)
ptr = 0 # available[ptr:] がまだ使える社員
success = True
for P, R, original_idx in projects:
if len(available) - ptr < R:
success = False
break
# 上位R人のスキルを合計
total = 0
selected = []
for i in range(R):
val = available[ptr + i]
total += val
selected.append(val)
result[original_idx] = total
# ptrを進める(このselectedは使ったことにする)
ptr += R
if not success:
print(-1)
else:
for val in result:
print(val)
if __name__ == "__main__":
main()
This editorial was generated by qwen3-coder-480b.