员工一共有n名(包括小度), 果汁一共有k种,第i位员工每个人都有自己最喜欢的一种果汁
。
小度到达果汁商店的时候,被告知商店正在举行促销活动,全场果汁买一送一(购买一瓶i型果汁,再赠送一瓶i型果汁)。
小度想让所有人(包括小度)都拿到他们最喜欢的果汁,需要购买多少瓶呢?
第一行两个整数,
,分别表示员工人数和果汁种类数。
第二行n个整数,表示i号员工最喜欢的果汁种类是
。
一个整数,表示小度需要购买的瓶数(赠品不算购买的)。
5 3 1 2 3 1 2
3
6 2 1 2 1 2 1 2
4
#include <bits/stdc++.h> using namespace std; int main() { int m, n, x; cin >> m >> n; unordered_map<int, int> umap; for (int i = 0; i < m; ++i) { cin >> x; umap[x]++; } int res = 0; for (auto [i, num]:umap) { res += (num >> 1) + num % 2; } cout << res << endl; return 0; }
#include<iostream> #include<vector> using namespace std; int main() { int n, k; scanf("%d %d", &n, &k); vector<int> hash(k,0); for(int i = 0; i < n; ++i) //统计所有员工喜爱饮料的频次 { int x; scanf("%d", &x); hash[x - 1]++; } int sum = 0; for(auto &x : hash) //根据频次来计算购买数量 { if(x != 0) { sum += (x + 1) / 2; } } cout << sum; return 0; }