E - プロジェクト配属 / Project Assignment Editorial by admin Editorial for printing
GPT 5.2 HighOverview
We simulate the process of assigning “the top \(R_j\) skilled employees among remaining employees” to projects in order of priority, doing so efficiently. If the assignment fails, we output \(-1\); otherwise, we compute the total skill for each project.
Analysis
Key Insight
The assignment rule is essentially a repeated operation of “take the highest-skilled employees among unassigned ones.”
If we sort employees by skill in descending order, then at any point, the “set of unassigned employees” is always a suffix of this sorted sequence. This is because we always take the required number of people from the top, so the consumed portion is always a contiguous prefix.
In other words, once we determine the order in which projects are processed (descending priority, and ascending project number for ties), the assignment becomes:
- The first project gets the top \(R\) skilled employees (the first \(R\) elements)
- The next project gets the next \(R\) employees (the next \(R\) elements immediately after)
- ……
This means we simply “consume from the front of the sorted skill array in order.”
Why the Naive Approach Is Dangerous
If we naively “extract the top \(R_j\) employees from unassigned ones” for each project, we would need sorting or max-extraction (heap) each time, resulting in a total of \(O\!\left(\sum R_j \log N\right)\), which can be large (since in the worst case we extract \(N\) elements).
However, thanks to the above insight, the extracted elements always form a contiguous interval of the sorted array, so we only need to efficiently compute interval sums.
How to Solve It
- Sort employee skills \(S\) in descending order
- Build a prefix sum array \(pref[i] = S_1 + \cdots + S_i\)
- Sort the projects according to the rules, then consume them from the front, taking the required number of people as an “interval,” and compute each interval sum in \(O(1)\) using the prefix sums
The failure condition (insufficient remaining employees) can also be checked easily by maintaining the number of employees used so far \(pos\): if \(pos + R_j > N\), we immediately determine failure.
Algorithm
- Sort the employee skill array \(S\) in descending order.
- Build the prefix sum array \(pref\):
- \(pref[0] = 0\)
- \(pref[i] = pref[i-1] + S[i-1]\) (\(i=1..N\))
- For each project \(j\), create an element \((-P_j, j, R_j)\) and put it in an array, then sort in ascending order.
This achieves “descending priority, ascending project number for ties.” - Prepare a pointer \(pos=0\) representing the number of employees used, and process the sorted projects in order:
- If \(pos + R_j > N\), the assignment fails, so output \(-1\) and terminate.
- Otherwise, the total skill assigned to this project is $\( pref[pos + R_j] - pref[pos] \)\( (the sum of the interval \)[pos, pos+R_j)\( in the sorted \)S$).
- Store the computed value in the answer array at the original project number \(j\), and update \(pos \leftarrow pos + R_j\).
- If all projects are processed successfully, output the answers in project number order \(1..M\).
(Example) If \(S=[10,8,5,5,1]\) and the required numbers of people in order are \(2,1,2\), then the assignments are intervals \([10,8]\), then \([5]\), then \([5,1]\), and the interval sums can be computed instantly using prefix sums.
Complexity
- Time complexity: \(O(N\log N + M\log M)\) (sorting employee skills and sorting projects)
- Space complexity: \(O(N + M)\) (prefix sums, project information, and answer array)
Implementation Notes
The sort key for projects is “descending priority, ascending number,” so in Python, inserting \((-P, j, R)\) and sorting in ascending order works easily.
Since the answer for each project must be “output in input order (by number),” even though we process them after sorting, we store the results back using the original number \(j\).
Interval sums are computed as \(pref[r]-pref[l]\) using prefix sums. Managing \(pos\) as “the next starting position to use” keeps the implementation simple.
Source Code
import sys
def main():
input = sys.stdin.buffer.readline
N, M = map(int, input().split())
S = list(map(int, input().split()))
S.sort(reverse=True)
projects = []
for j in range(1, M + 1):
P, R = map(int, input().split())
projects.append((-P, j, R)) # sort by P desc, then index asc
projects.sort()
pref = [0] * (N + 1)
for i, v in enumerate(S, 1):
pref[i] = pref[i - 1] + v
ans = [0] * (M + 1)
pos = 0
for _, idx, r in projects:
if pos + r > N:
sys.stdout.write("-1\n")
return
ans[idx] = pref[pos + r] - pref[pos]
pos += r
sys.stdout.write("\n".join(str(ans[i]) for i in range(1, M + 1)) + "\n")
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
posted:
last update: