A - 屋台の営業日数 / Number of Days a Food Stall Is Open 解説 by admin
gemini-3-flash-thinkingOverview
Starting with initial funds of \(S\) yen, the problem asks to find the maximum number of days you can operate without your balance going negative, given daily sales (sum of product popularities) and expenses (sum of part-time worker daily wages).
Analysis
First, let’s organize the daily income and expenses. - Let the total daily sales be \(X = \sum_{i=1}^{N} A_i\). - Let the total daily payments be \(Y = \sum_{j=1}^{M} B_j\).
The change in balance after each day of business is \(X - Y\) yen. The balance at the end of day \(k\) can be expressed as: $\(\text{Balance} = S + k \times (X - Y)\)$
Now, let’s consider how many days the condition “balance at the end of business is at least \(0\) yen” can be maintained.
1. When income is positive or zero (\(X \geq Y\))
If the daily net income \(X - Y\) is \(0\) or more, the balance does not decrease as days pass.
Since the initial balance \(S\) is at least \(0\), and daily sales \(X\) are received before paying expenses \(Y\), the first business day can always be survived. After that, the balance is maintained or increases, so business can continue forever. Therefore, in this case, output -1.
2. When income is negative (\(X < Y\))
Since the balance decreases each day, eventually business can no longer continue. Let the daily loss be \(L = Y - X\) (\(L > 0\)). The balance after \(k\) days is \(S - k \times L\). This must be at least \(0\): $\(S - k \times L \geq 0\)\( \)\(k \times L \leq S\)\( \)\(k \leq \frac{S}{L}\)\( The maximum integer \)k\( satisfying this is the quotient of \)S\( divided by \)L$ (rounded down).
Algorithm
- Compute the sum of product popularities \(X = \sum A_i\) and the sum of daily wages \(Y = \sum B_j\).
- If \(X \geq Y\), output
-1and terminate. - If \(X < Y\), compute the loss \(L = Y - X\).
- Compute the quotient \(S // L\) and output that value.
Complexity
- Time complexity: \(O(N + M)\)
- Computing the sum of \(A_i\) and the sum of \(B_j\) requires \(N\) and \(M\) additions, respectively.
- Space complexity: \(O(N + M)\)
- Required when storing the input values as lists.
Implementation Notes
Large input sizes: Since \(N\) and \(M\) can be as large as \(10^5\), in Python it is efficient to read input quickly using methods like
sys.stdin.read().split().Range of values: Since \(S\), \(A_i\), and \(B_j\) can be large, the sums and intermediate calculations may exceed \(10^{18}\). Python handles arbitrary-precision integers natively, so there is no concern about overflow.
Floor division: When dividing \(S\) by \(L\), Python’s
//operator conveniently computes the integer part (floor division).Source Code
import sys
def solve():
# Fast I/O: read all input at once and split into tokens
# This is efficient for handling the large number of inputs (up to 2*10^5 integers).
try:
input_data = sys.stdin.read().split()
except EOFError:
return
if not input_data:
return
# N: Number of items, M: Number of part-time workers, S: Initial capital
n = int(input_data[0])
m = int(input_data[1])
s = int(input_data[2])
# Popularity A_i corresponds to the daily sales from the i-th item.
# Daily wage B_j is the amount paid to the j-th worker.
# The total daily sales is the sum of all A_i.
# The total daily expense is the sum of all B_j.
# A_i values are located from index 3 to 3+n-1 in the input_data list.
# B_j values are located from index 3+n to 3+n+m-1.
# Using sum(map(int, ...)) is efficient in Python for summing a slice of the input.
a_sum = sum(map(int, input_data[3:3+n]))
b_sum = sum(map(int, input_data[3+n:3+n+m]))
# The problem states that each day, sales are earned first, and then wages are paid.
# Business is successful for a day if the balance after paying wages is non-negative.
# Let S_k be the balance after k days.
# S_k = S + k * (a_sum - b_sum)
# We need S_k >= 0 for all k from 1 to the maximum business days.
diff = a_sum - b_sum
if diff >= 0:
# If daily sales are greater than or equal to daily wages, the balance
# will not decrease over time. Since S >= 0 and a_sum >= b_sum,
# S_1 = S + (a_sum - b_sum) will always be >= 0, and subsequent days
# will also maintain a non-negative balance.
print("-1")
else:
# If daily wages are greater than daily sales, the balance decreases each day.
# Let loss = b_sum - a_sum (where loss > 0).
# The balance after k days is S - k * loss.
# We need S - k * loss >= 0, which implies k <= S / loss.
# The maximum integer k satisfying this is the floor of S / loss.
loss = b_sum - a_sum
# In Python, // performs floor division.
print(s // loss)
if __name__ == '__main__':
solve()
This editorial was generated by gemini-3-flash-thinking.
投稿日時:
最終更新: