A - アルバイトの給料計算 / Calculating Part-Time Job Pay 解説 by admin
Gemini 3.1 Pro (Thinking)Overview
Given the number of shifts for each of \(N\) staff members, this problem asks you to calculate the salary for each person — adding a special bonus when the condition is met — and find the total amount for all staff.
Analysis
This problem can be solved by directly implementing the salary calculation rules described in the problem statement. Looking at the constraints, the number of staff \(N\) is at most \(100\), which is very small, so a straightforward approach of calculating each person’s salary one by one and summing them up is more than fast enough (it will not exceed the time limit).
Given that the number of shifts for each staff member is \(C_i\), the salary paid is as follows: - If \(C_i \ge K\): A special bonus is added, so each shift pays \(P + B\) yen. The payment to that staff member is \(C_i \times (P + B)\) yen. - If \(C_i < K\): Only the base pay applies, so each shift pays \(P\) yen. The payment to that staff member is \(C_i \times P\) yen.
We simply compute this for all staff members and sum the results. The maximum possible total salary is around \(100 \times 31 \times (1000 + 1000) = 6,200,000\) yen, so it fits within a standard 32-bit integer type (int) even in languages like C++, and there is no concern about overflow.
Algorithm
- Initialize a variable
totalto \(0\) to store the answer. - For each staff member’s shift count \(C_i\), perform the following:
- If \(C_i \ge K\), add \(C_i \times (P + B)\) to
total. - Otherwise, add \(C_i \times P\) to
total.
- If \(C_i \ge K\), add \(C_i \times (P + B)\) to
- Finally, output
total.
Complexity
- Time complexity: \(O(N)\)
- For each of the \(N\) staff members, we perform one conditional branch and one multiplication, so the processing time is proportional to \(N\).
- Space complexity: \(O(N)\)
- This is used to store the shift counts for all \(N\) staff members in an array (list).
Implementation Notes
In Python, you can combine the sum() function with a generator expression to write the loop and conditional branching concisely.
The expression c * (P + B) if c >= K else c * P in the provided solution code represents the logic “if \(c \ge K\) then \(c \times (P + B)\), otherwise \(c \times P\)” in a single line.
Of course, writing it with a standard for loop and if statement as shown below works perfectly fine and is more intuitive to understand.
total = 0
for c in C:
if c >= K:
total += c * (P + B)
else:
total += c * P
Source Code
N, P, B, K = map(int, input().split())
C = list(map(int, input().split()))
total = sum(c * (P + B) if c >= K else c * P for c in C)
print(total)
This editorial was generated by gemini-3.1-pro-thinking.
投稿日時:
最終更新: