公式

A - サービス券 / Service Tickets 解説 by admin 印刷ページ

gpt-6-astra-medium

Overview

For each customer, calculate the number of service vouchers from their payment amount, capped at the maximum of \(M\), and sum them up.

Analysis

Since \(1\) voucher is given for every \(100\) yen, the number of vouchers given to the \(i\)-th customer without considering the limit is \(\lfloor A_i / 100 \rfloor\). Here, \(\lfloor x \rfloor\) denotes the value obtained by rounding down \(x\) to the nearest integer.

However, since at most \(M\) vouchers can be given to a single customer, the actual number of vouchers given is:

\(\min(\lfloor A_i / 100 \rfloor, M)\)

For example, when \(M = 3\):

  • A customer who paid \(250\) yen receives \(2\) vouchers.
  • A customer who paid \(480\) yen would normally receive \(4\) vouchers, but receives \(3\) vouchers due to the limit.
  • A customer who paid \(90\) yen receives \(0\) vouchers.

In this case, the total is \(2 + 3 + 0 = 5\) vouchers.

An important point to note is that the rounding down and capping must be applied to each customer individually. For instance, even if there are \(2\) customers who each paid \(50\) yen, the number of vouchers given to each is \(0\), resulting in a total of \(0\) vouchers. If you sum up the payment amounts first and then divide by \(100\), it would incorrectly calculate \(1\) voucher.

A simple approach that processes each customer in order is fast enough to solve this problem.

Algorithm

  1. Read \(N, M\) and the payment amount of each customer.
  2. For each payment amount \(A_i\), compute \(A_i // 100\) using integer division.
  3. Take the minimum of that value and \(M\) as the number of vouchers given to that customer.
  4. Sum the number of vouchers given to all customers, and print it.

In other words, the answer to be found is given by the following formula:

\(\displaystyle \sum_{i=1}^{N} \min(\lfloor A_i / 100 \rfloor, M)\)

Since the number of vouchers given to each customer is determined according to the rules in the problem statement and summed up, this calculation yields the correct total number of vouchers.

Complexity

  • Time Complexity: \(O(N)\) — We perform constant-time calculations for each customer.
  • Space Complexity: \(O(N)\) — In the provided code, the input data and the list of payment amounts are stored.

Implementation Notes

  • In Python, // represents integer division. Since the payment amounts are positive integers, x // 100 computes the quotient rounded down.

  • min(x // 100, m) applies the limit per customer.

  • sum(min(x // 100, m) for x in a) computes the total number of vouchers given to all customers.

    Source Code

# 問題文から独立に実装: 各客について A_i // 100 枚、ただし上限 M 枚。
import sys

def main():
    data = sys.stdin.read().split()
    n, m = int(data[0]), int(data[1])
    a = list(map(int, data[2:2 + n]))
    print(sum(min(x // 100, m) for x in a))

main()

This editorial was generated by gpt-6-astra-medium.

投稿日時:
最終更新: