公式

A - 階段の水流 / Waterflow on Stairs 解説 by admin 印刷ページ

Qwen3-Coder-480B

Overview

This problem asks you to simulate \(Q\) operations of exposing specified flower pots to sunlight, arranged in \(N\) steps like stairs, and determine the final amount of water in each pot.

Analysis

Problem Breakdown

The sunlight exposure operation is performed in two steps: 1. Up to \(B_i\) milliliters of water evaporates from pot \(i\) 2. All remaining water flows down to pot \(i+1\) (pot \(i\) becomes empty)

Key Observations

  • Water flows only in one direction, from top to bottom
  • Each operation affects only two pots: the pot \(i\) exposed to sunlight and the pot \(i+1\) below it
  • Operations do not cascade to affect multiple pots in a chain

Is a Naive Approach Sufficient?

Since each operation can be processed in constant time \(O(1)\), naively simulating \(Q\) operations results in \(O(Q)\) overall. This is fast enough given the constraints \(N, Q \leq 2 \times 10^5\).

Algorithm

  1. Initialize array water with initial values \(A\)
  2. For each operation \(c\), execute the following:
    • Get the current water amount in pot \(c\)
    • Calculate evaporation: \(\min(\text{current water amount}, B_c)\)
    • Calculate remaining water: \(\text{current water amount} - \text{evaporation}\)
    • Set water in pot \(c\) to \(0\)
    • If \(c < N\), add the remaining water to pot \(c+1\)
  3. Output the final water array

Concrete Example

For \(N=3\), \(A=[10, 5, 3]\), \(B=[3, 2, 1]\), when exposing pot 1 to sunlight:

  • Water in pot 1: 10 ml
  • Evaporation: \(\min(10, 3) = 3\) ml
  • Remaining: \(10 - 3 = 7\) ml flows to pot 2
  • Result: \([0, 12, 3]\)

Complexity

  • Time Complexity: \(O(N + Q)\)

    • \(O(N)\) for reading the initial array
    • \(O(1)\) × \(Q\) operations = \(O(Q)\)
  • Space Complexity: \(O(N)\)

    • \(O(N)\) for the array water that manages water amounts

Implementation Notes

  1. 0-indexed vs 1-indexed conversion: In the problem statement, pots are numbered starting from 1, but arrays start from 0, so conversion with i = c - 1 is necessary.

  2. Boundary condition handling: When \(i = N\) (the last pot), water flows into the drain and disappears, so don’t forget to check i + 1 < N.

  3. Evaporation calculation: If water is less than \(B_i\), all of it evaporates, so calculate evaporation with min(current_water, B[i]).

Source Code

import sys
from collections import defaultdict

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))
    
    # Count how many times each pot is exposed to sunlight
    # and track the order of operations for each pot
    pot_operations = defaultdict(list)
    for j, c in enumerate(C):
        pot_operations[c - 1].append(j)  # 0-indexed
    
    # For each pot, we need to track:
    # - When it receives sunlight, water evaporates and flows down
    # - When water flows in from above
    
    # Let's simulate but optimize by processing in order
    # Key insight: water only flows downward, so we can process from top to bottom
    
    # For each pot i, we need to know all times it's exposed to sunlight
    # Between consecutive exposures, water can accumulate from pot i-1
    
    # Let's think differently:
    # Process all Q operations, but track water flow efficiently
    
    # Actually, let's just simulate but be smart about it
    # When pot i is exposed, water flows to pot i+1
    # We need to handle this correctly
    
    # Simple simulation should work for N, Q <= 2*10^5 if we're careful
    # Each operation only affects one pot directly, but water flows down
    
    water = A[:]
    
    for c in C:
        i = c - 1  # 0-indexed
        current_water = water[i]
        evaporated = min(current_water, B[i])
        remaining = current_water - evaporated
        water[i] = 0
        if i + 1 < N:
            water[i + 1] += remaining
        # if i + 1 == N, water goes to drain (disappears)
    
    print(' '.join(map(str, water)))

if __name__ == '__main__':
    main()

This editorial was generated by claude4.5opus.

投稿日時:
最終更新: