A - サービス券 / Service Tickets 解説 by admin 印刷ページ
gpt-6-astra-mediumOverview
For each customer, find the minimum of the number of service tickets calculated from their payment amount and the upper limit \(M\). The sum of these values over all customers will be the answer.
Analysis
Without the upper limit, the number of service tickets given to the \(i\)-th customer would be \(\left\lfloor A_i / 100 \right\rfloor\). Here, \(\lfloor x \rfloor\) denotes the value obtained by rounding down \(x\) (truncating the decimal part).
However, since the maximum number of tickets that can be given to one person is \(M\), the actual number of tickets given is
\(\min\left(\left\lfloor A_i / 100 \right\rfloor, M\right)\)
For example, when \(M = 3\):
- A customer who pays \(250\) yen receives \(2\) tickets.
- A customer who pays \(580\) yen would normally receive \(5\) tickets, but due to the upper limit, they receive \(3\) tickets.
- A customer who pays \(90\) yen receives \(0\) tickets.
In this case, the total is \(2 + 3 + 0 = 5\) tickets.
Do not sum up the payment amounts for all customers before dividing by \(100\). Truncating and applying the upper limit must be done for each customer individually. For example, if there are two customers who each pay \(50\) yen, the total number of tickets given is \(0\), but summing the payments first would incorrectly yield \(1\) ticket.
A straightforward approach of processing each customer sequentially is fast enough, so no special optimization is needed.
Algorithm
- Read \(N\) and \(M\), and initialize the total number of tickets
totalto \(0\). - For each customer, perform the following steps:
- Read the payment amount \(A_i\).
- Compute \(A_i / 100\) using integer division and assign it to
tickets. - If
ticketsis greater than \(M\), replace it with \(M\). - Add
ticketstototal.
- Print
total.
By calculating the number of tickets for each customer according to the rules in the problem statement and summing them all up, the correct total number of tickets is obtained.
Complexity
- Time complexity: \(O(N)\)
A constant number of operations are performed for each customer. - Space complexity: \(O(1)\)
The payment amounts are processed as they are read, without storing them in an array.
Implementation Notes
The presented code is in C++. In C++, division between positive integers
a / 100yields the quotient rounded down.The upper limit \(M\) applies to the number of tickets given to each customer, not the total number of tickets.
Since the answer is at most \(N \times M \leq 10000\), the total number of tickets can be handled with a standard
inttype.Source Code
// 問題文から独立に実装: 100円ごとに1枚、1人あたり M 枚が上限。
#include <iostream>
int main() {
int n, m;
std::cin >> n >> m;
int total = 0;
for (int i = 0; i < n; ++i) {
int a;
std::cin >> a;
int tickets = a / 100;
if (tickets > m) tickets = m;
total += tickets;
}
std::cout << total << std::endl;
}
This editorial was generated by gpt-6-astra-medium.
投稿日時:
最終更新: