C - 花壇の植え替え / Replanting the Flower Bed 解説 by admin
gemini-3-flash-thinkingOverview
This problem asks us to find the minimum number of operations to unify all flowers in an \(N \times M\) grid to the same variety, by keeping the most frequently planted variety and replanting all other plots.
Analysis
Consider unifying all plots to a specific variety \(X\). The number of operations required can be expressed as:
\[\text{Number of operations} = (\text{Total number of plots}) - (\text{Number of plots already planted with variety } X)\]
To minimize the number of operations, we need to maximize the “number of plots already planted with variety \(X\)” on the right-hand side.
For example, consider a \(2 \times 2\) flower bed with varieties \(\{1, 2, 2, 3\}\) planted: - Unifying to variety \(1\): \(4 - 1 = 3\) operations needed - Unifying to variety \(2\): \(4 - 2 = 2\) operations needed - Unifying to variety \(3\): \(4 - 1 = 3\) operations needed
As shown, unifying to the most frequently occurring variety (variety \(2\) in this example) is optimal.
Since variety numbers \(A_{i,j}\) can be as large as \(10^9\), they cannot be used directly as array indices. Therefore, we need to use an associative array (in Python, a dictionary or collections.Counter) to count the occurrences of each variety.
Algorithm
- Read \(N, M\) and all variety numbers \(A_{i,j}\) from input.
- Calculate the total number of plots \(S = N \times M\).
- Count the occurrences of each variety number using a hash map (dictionary) or similar data structure.
- Let \(max\_freq\) be the maximum occurrence count.
- Output \(S - max\_freq\) as the answer.
Complexity
- Time complexity: \(O(NM)\)
- Since we scan and count the variety of each plot once, it runs in linear time with respect to the number of plots \(NM\).
- Space complexity: \(O(NM)\)
- The hash map recording variety occurrence counts can be at most size \(NM\).
Implementation Notes
Fast input: Since \(N \times M\) can be as large as \(5 \times 10^5\), reading all input at once using
sys.stdin.read().split()is faster than callinginput()repeatedly.Counting occurrences: In Python,
collections.Countercan efficiently count element occurrences from a list.Skipping type conversion: Since we don’t use the variety numbers in calculations and only need to identify “whether they are the same type,” we can count the read strings directly without converting them to
int(), which reduces execution time.Source Code
import sys
from collections import Counter
def solve():
"""
すべての区画の花を同じ品種にそろえるために必要な操作回数の最小値を求める。
アルゴリズム:
1. 花壇にあるすべての品種の出現回数をカウントする。
2. 最も多く出現する品種の回数 (max_freq) を特定する。
3. 全区画数 (N * M) から max_freq を引いた値が、変更が必要な最小の操作回数となる。
"""
# 標準入力からすべてのデータを読み込み、空白で分割してリストにする
# sys.stdin.read().split() は大量の入力を高速に読み込むのに適している
input_data = sys.stdin.read().split()
if not input_data:
return
# 最初の2つの要素は行数 N と列数 M
n = int(input_data[0])
m = int(input_data[1])
# 全体の区画数
total_cells = n * m
# 3番目以降の要素が各区画の品種番号
# リストのスライスを用いて品種番号の部分のみを取り出す
varieties = input_data[2:]
if not varieties:
# 制約上 N, M >= 1 であるため、通常ここには到達しない
print(0)
return
# collections.Counter を使用して各品種の出現回数を効率的にカウントする
# 品種番号は整数のままでも文字列のままでもカウント可能であるため、
# 変換オーバーヘッドを避けるために文字列のまま処理する
counts = Counter(varieties)
# 最も頻繁に出現する品種の出現回数を取得する
max_freq = max(counts.values())
# 最小操作回数 = (全区画数) - (最も多い品種の出現回数)
print(total_cells - max_freq)
if __name__ == '__main__':
solve()
This editorial was generated by gemini-3-flash-thinking.