A - イベント払い戻し / Event Refund 解説 by admin 印刷ページ
Claude 4.6 Opus (Thinking)Overview
For each of the \(N\) visitors, calculate the refund amount based on the ticket type, and find the total. For a regular ticket (normal), the full price is refunded; for a student discount ticket (half), half the price (rounded down) is refunded.
Analysis
This problem is very straightforward — you simply need to branch on the condition for each visitor and accumulate the refund amounts.
- Key observation: There are only 2 ticket types,
normalandhalf, so a simpleif-elseis sufficient. - Rounding down: For student discount tickets, the refund is half the purchase price, rounded down (floor division). For example, if the purchase price is \(P = 1001\) yen, the refund is \(\lfloor 1001 / 2 \rfloor = 500\) yen. In Python, this can be achieved using the integer floor division operator
//. - No special tricks are needed for this problem — a naive loop over all \(N\) visitors is perfectly sufficient. Since \(N \leq 2 \times 10^5\), an \(O(N)\) solution runs with plenty of room.
Concrete Example
Consider the following input:
3
normal 1000
half 1500
half 1001
| Visitor | Ticket Type | Purchase Price | Refund Amount |
|---|---|---|---|
| 1 | normal |
1000 | \(1000\) (full price) |
| 2 | half |
1500 | \(\lfloor 1500 / 2 \rfloor = 750\) |
| 3 | half |
1001 | \(\lfloor 1001 / 2 \rfloor = 500\) |
The total refund amount is \(1000 + 750 + 500 = 2250\).
Algorithm
- Read the number of visitors \(N\).
- Initialize a variable
totalto \(0\) to track the total amount. - Read the data for each of the \(N\) visitors one by one, and perform the following:
- If the ticket type is
normal, add the purchase price \(P_i\) directly tototal. - If the ticket type is
half, add \(\lfloor P_i / 2 \rfloor\) tototal.
- If the ticket type is
- Output the final value of
total.
Complexity
- Time complexity: \(O(N)\) — constant-time processing is performed for each visitor.
- Space complexity: \(O(1)\) — only a variable to hold the total amount is used; there is no need to store all the data.
Implementation Notes
Floor division: In Python, the
//operator performs integer floor division. As long as \(P_i\) is a positive integer,p // 2correctly gives the half price rounded down.Parsing input: Each line contains the ticket type (string) and the purchase price (integer) separated by a space, so you can split with
input().split()and then convert only the price part to an integer usingint().Conditional branching: Since the ticket type is either
normalorhalf, it is sufficient to checkif t == "normal"and treat everything else ashalf.Source Code
N = int(input())
total = 0
for _ in range(N):
t, p = input().split()
p = int(p)
if t == "normal":
total += p
else:
total += p // 2
print(total)
This editorial was generated by claude4.6opus-thinking.
投稿日時:
最終更新: