公式

C - Socks 解説 by en_translator


Consider the socks of each color. When there are \(C\) socks of one color, one can pair them at most \(\lfloor \frac{C}{2} \rfloor\) times. Therefore, it is sufficient to manage “how many socks of each color are there?” using a structure like std::map.

Sample code (C++):

”` #include

using namespace std;

int main() { int n; cin >> n; map mp; for (int i = 0; i < n; i++) { int a; cin >> a; ++mp[a]; }

int ans = 0;
for (auto [_, cnt]: mp) ans += cnt / 2;
cout << ans << endl;

}

投稿日時:
最終更新: