D - お土産の配布 / Distribution of Souvenirs 解説 by admin
GPT 5.2 HighOverview
Since each type of snack “cannot give 2 or more to the same member,” we consider the upper limit of distribution per type and determine whether we can distribute a total of \(N \times K\) snacks to everyone.
Analysis
The key point is “snack type \(i\) can be distributed to at most \(N\) people.”
- Even if \(C_i\) is very large, since you cannot give 2 of the same type to the same person,
type \(i\) can only be distributed as “at most 1 per person to \(N\) people.” - Therefore, the actual number of snacks usable from type \(i\) for distribution is
$\( \min(C_i,\,N) \)$
To distribute to everyone, we need \(N\) people × \(K\) snacks each, i.e., $\( \text{need} = N \times K \)$ snacks in total.
On the other hand, the total available for distribution is $\( \text{supply} = \sum_{i=1}^{M} \min(C_i,\,N) \)$
- If \(\text{supply} < \text{need}\), it is impossible no matter what, since the total is insufficient.
- If \(\text{supply} \ge \text{need}\), it is possible. This is because each type can be given to any member and the only constraint is “each type can be given to the same member at most once,” so if the total “slots” of distributing up to \(N\) people per type is at least \(N K\), we can fill the \(K\) slots for every member.
Naively simulating “who gets what” is impossible since \(N\) can be up to \(10^9\), making it infeasible to even hold an array (TLE/memory issues). All we need is to check the total.
Concrete example:
- \(N=3, K=2\) (6 snacks needed in total)
- \(C=[10,1,1]\)
- \(\min(C_i,N)=[3,1,1]\) so \(\text{supply}=5\), not enough → No
Algorithm
- Compute the required number \(\text{need} = N \times K\).
- For each type \(i\), accumulate \(\min(C_i, N)\) to obtain \(\text{supply}\).
- If \(\text{supply} \ge \text{need}\), output
Yes; otherwise, outputNo.
(In the provided code, it terminates early as soon as \(\text{supply} \ge \text{need}\) is reached.)
Complexity
- Time complexity: \(O(M)\)
- Space complexity: \(O(1)\) (constant amount beyond the input array)
Implementation Notes
The contribution per type must always be \(\min(C_i, N)\) (even if \(C_i\) exceeds \(N\), only \(N\) can be used).
\(\text{need}=N\times K\) can become very large, so be careful with 64-bit integers (especially in other languages). In Python, integers are automatically arbitrary precision, but early termination makes it faster.
Source Code
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
if not data:
return
N, M, K = data[0], data[1], data[2]
C = data[3:3 + M]
need = N * K
supply = 0
for ci in C:
supply += ci if ci < N else N
if supply >= need:
print("Yes")
return
print("No")
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
投稿日時:
最終更新: