Official

C - 畑の均し作業 / Leveling the Field Editorial by admin

Qwen3-Coder-480B

Overview

We find the minimum number of operations to level the soil height of each section to the maximum value, where each operation adds soil to a contiguous interval.

Analysis

In this problem, we need to make the soil height \(A_i\) of each section equal to the maximum value \(\max(A)\). Since a single operation can add soil of height \(1\) to a contiguous interval, we can add soil efficiently without operating on each section individually.

For example, suppose we have the following input:

A = [1, 2, 1, 3]

The maximum value is \(3\), so we need to add soil to each section as follows:

diff = [2, 1, 2, 0]

The key point here is that “we can add soil to contiguous sections simultaneously.” In other words, if a contiguous interval requires the same number of soil additions, we can treat them together as a single operation.

This is where the technique of “focusing on the cumulative increases in the differences” comes in. Specifically, we consider the difference array \(D[i] = \max(A) - A[i]\), and when scanning from left to right, the increasing parts (i.e., parts where more soil needs to be added compared to the previous section) correspond to the number of new operations needed.

For example, considering the above case:

  • \(D = [2, 1, 2, 0]\)
  • First operation adds soil once to the entire interval → \(D = [1, 0, 1, 0]\)
  • Similarly, add soil once more to the entire interval → \(D = [0, 0, 0, 0]\)

In this way, the number of operations can be computed as \(D[0] + \sum_{i=1}^{N-1} \max(0, D[i] - D[i-1])\).

A naive simulation would require updating intervals for each operation, which would not be fast enough. However, with this analysis, we can efficiently find the answer by looking at the increases in the differences.

Algorithm

  1. Read the heights \(A_i\) of each section from the input and find the maximum value \(\max(A)\).
  2. For each section, compute the difference to the target: \(D[i] = \max(A) - A[i]\).
  3. Initialize the operation count and add \(D[0]\).
  4. For \(i = 1\) to \(N-1\), if \(D[i] > D[i-1]\), add the difference \(D[i] - D[i-1]\) to the operation count.
  5. Output the final operation count.

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(N)\) (when storing the difference array \(D\); can be optimized in the code)

Implementation Notes

  • sys.stdin.read is used for fast input reading.

  • Instead of explicitly storing the difference array, we can keep only the previous value to reduce the space complexity to \(O(1)\).

  • There are no constraints that require worrying about overflow, and in Python this is not a concern anyway.

    Source Code

import sys
input = sys.stdin.read

def main():
    data = input().split()
    N = int(data[0])
    A = list(map(int, data[1:N+1]))
    
    # 最大値を求める
    max_h = max(A)
    
    # 各区画ごとに、何回その区画に土を追加する必要があるかを計算
    # これは max_h - A[i] 回
    # しかし、連続区間に対して同時に作業できるので、
    # 差分の累積を見て、何回の操作が必要かを考える
    
    # 差分配列 D を定義: D[i] = max_h - A[i]
    D = [max_h - a for a in A]
    
    # 操作回数は、Dの「増加する箇所」の合計
    # つまり、D[i] > D[i-1] のとき、D[i] - D[i-1] の操作が必要
    
    operations = D[0]
    for i in range(1, N):
        if D[i] > D[i-1]:
            operations += D[i] - D[i-1]
    
    print(operations)

if __name__ == "__main__":
    main()

This editorial was generated by qwen3-coder-480b.