A - サービス券 / Service Tickets 解説 by admin 印刷ページ
swe-2-highExplanation
For each customer \(i\), the payment of \(A_i\) yen would normally earn \(\left\lfloor \dfrac{A_i}{100} \right\rfloor\) tickets, but the number given to one person is capped at \(M\). Therefore, customer \(i\) receives
\[\min\left(\left\lfloor \frac{A_i}{100} \right\rfloor,\ M\right)\]
tickets. The answer is the sum of this quantity over all customers.
Algorithm
- Read \(N\) and \(M\).
- For each \(A_i\), add \(\min(A_i \mathbin{\mathrm{div}} 100,\ M)\) to a running total (integer division automatically truncates the fractional part).
- Print the total.
Proof of Correctness
Claim: The output equals \(\sum_{i=1}^{N} \min\left(\left\lfloor A_i/100 \right\rfloor, M\right)\).
Proof: We show the loop invariant that after the \(i\)-th iteration, total equals \(\sum_{j=1}^{i} \min(\lfloor A_j/100 \rfloor, M)\).
- Initialization: Before the loop,
total = 0, which equals the empty sum. - Maintenance: In the \(i\)-th iteration we read \(A_i\) and add \(\min(A_i/100, M)\) to
total. Since integer division on non-negative integers in both C++ and Python yields \(\lfloor A_i/100 \rfloor\), the invariant is preserved. - Termination: After \(N\) iterations,
totalequals the total number of tickets distributed to all customers.
The problem’s rule — “one ticket per 100 yen, at most \(M\) tickets per person” — is exactly \(\min(\lfloor A_i/100 \rfloor, M)\) per customer, and the grand total is the sum of independent per-customer amounts, so this value is the correct answer.
Complexity
- Time: \(O(N)\), including reading the input.
- Space: \(O(1)\) for C++ and \(O(N)\) for Python, including stored input and output arrays or strings.
Tie-breaking
The answer is uniquely determined, so there is no tie-breaking.
投稿日時:
最終更新: