Official

C - 花壇の植え替え / Replanting the Flower Bed Editorial by admin Editorial for printing

GPT 5.4 High

Overview

To make all plots the same variety, you choose one variety to unify everything to, and replant all plots that don’t already have that variety.
Therefore, keeping “the variety that is currently planted the most” minimizes the number of operations.

Analysis

The operation in this problem is “change one plot to any variety you like.”
That is, if you decide to unify the entire flower bed to some variety \(x\), then:

  • Plots that already have \(x\) planted can be left as they are
  • Only plots with varieties other than \(x\) need to be replanted

If the total number of plots in the flower bed is \(N \times M\) and the number of occurrences of variety \(x\) is \(cnt_x\), then the number of operations needed to unify to variety \(x\) is

\(N \times M - cnt_x\)

Therefore, to minimize this value, we need to maximize \(cnt_x\).
In other words:

  • Count how many times each variety appears
  • Find the maximum occurrence count among them
  • The answer is \(N \times M - (\text{maximum occurrence count})\)

Concrete Example

For example, suppose the flower bed is

\( \begin{matrix} 1 & 2 & 1 \\ 3 & 1 & 2 \end{matrix} \)

There are \(6\) plots in total, and the occurrence counts for each variety are:

  • Variety \(1\): 3 times
  • Variety \(2\): 2 times
  • Variety \(3\): 1 time

If we unify to variety \(1\), we only need to replant the remaining \(3\) plots, so the answer is \(6 - 3 = 3\).
This is fewer than unifying to any other variety, so this is the minimum.

Naive Approach and Its Issues

A naive approach would be to “try every possible variety to unify to.”
However, variety numbers can be up to \(10^9\), and it is impossible to try all of them including non-existent varieties.

The key insight is that the only varieties worth considering as the final choice are “varieties that already appear.”
This is because unifying to a completely new variety that doesn’t appear at all would require replanting every single plot, resulting in an operation count of always \(N \times M\).
This can never be better than unifying to a variety that appears at least once.

Therefore, it suffices to only count the occurrences of varieties that actually appear.

Algorithm

  1. Prepare a dictionary cnt and count the number of occurrences of each variety.
  2. At the same time, keep updating the maximum occurrence count max_cnt.
  3. Finally, output \(N \times M - max\_cnt\).

With this method, the answer can be determined by looking at each plot exactly once.

Complexity

  • Time complexity: \(O(NM)\)
  • Space complexity: \(O(K)\)

where \(K\) is the number of distinct varieties appearing in the flower bed.
In the worst case \(K \leq NM\), so the space complexity can be considered \(O(NM)\).

Implementation Notes

  • Since variety numbers can be up to \(10^9\), we manage counts using a dictionary dict instead of an array.

  • Because the input size can be large, we use sys.stdin.buffer.readline for fast reading.

  • By updating max_cnt each time we count an occurrence, we avoid the need to traverse the entire dictionary again at the end.

    Source Code

import sys

def main():
    input = sys.stdin.buffer.readline
    N, M = map(int, input().split())
    cnt = {}
    max_cnt = 0

    for _ in range(N):
        for x in map(int, input().split()):
            c = cnt.get(x, 0) + 1
            cnt[x] = c
            if c > max_cnt:
                max_cnt = c

    print(N * M - max_cnt)

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.4-high.

posted:
last update: