Official
C - 花壇の植え替え / Replanting the Flower Bed Editorial
by
C - 花壇の植え替え / Replanting the Flower Bed Editorial
by
MMNMM
Editorial for printing
整数 \(X\) に対して、品種番号 \(X\) の花が植えられている区画の個数を \(C _ X\) と書くことにします。 すべての区画に品種番号 \(X\) の花が植えられているようにするためには、\(NM-C _ X\) 回の操作が必要です。
よって、求める答えは \(\displaystyle NM-\max _ {X\in\mathbb Z}C _ X\) となります。 \(C _ X\) を連想配列などを用いて管理することで、この問題を解くことができます。
実装例は以下のようになります。
#include <iostream>
#include <map>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
map<int, int> count;
int max_count = 0;
for (int i = 0; i < N * M; ++i) {
int A;
cin >> A;
++count[A]; // A が植えられている区画の個数を増やして
max_count = max(count[A], max_count); // 最大値を更新
}
cout << N * M - max_count << endl;
return 0;
}
N, M = map(int, input().split())
count = {}
max_count = 0
for i in range(N):
for A in map(int, input().split()):
if A not in count:
count[A] = 0
count[A] += 1 # A が植えられている区画の個数を増やして
max_count = max(count[A], max_count) # 最大値を更新
print(N * M - max_count)
posted:
last update:
