Heng_Xin 题解分享 · 2024/5/9
班级活动(编程题) - 题解
直接贪心即可, 代码已经在[P9421 [蓝桥杯 2023 国 B] 班级活动 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)](https://www.luogu.com.cn/problem/P9421)中AC了 具体题解可以看代码的注释❤️ (不知道为什么这里只能拿60分qwq..) ```CPP #include <iostream> #include <string> #include <vector> #include <unordered_map> using namespace std; using ll = long long; int main() { int n; cin >> n; unordered_map<int, int> hash; /*贪心即可 对于 id计数 > 2 的, 可以分配给 id = 1 的 这样只需要花费一次 对于 id 计数= 1 的, 同上 对于剩下 如果剩下 id计数 > 2 的部分, 得2个人花费2次进行安置 对于 id计数 = 1 的部分, 也只是2个人需要花费1次 对于编号, 不用担心不足, 因为最多才有 n/2 对 */ for (int i = 0; i < n; ++i) { int c; cin >> c; ++hash[c]; } // 统计 ll res = 0; ll d2 = 0, d1 = 0; // 大于二的 / 等于 1 的 for (auto it : hash) { int id, cnt; tie(id, cnt) = it; if (cnt != 2) { if (cnt > 2) d2 += cnt - 2; else ++d1; } } /* 8 1 2 2 3 3 3 3 3 理论上输出 1 2 2 3 3 1 4 4 -> 3 d1: 1 -> 0 d2: 3 -> 2 6 1 2 3 4 5 6 d1: 6 / 2 -> 3 */ // 得到已经组好队的人数 if (d2 && d1) res += min(d2, d1); d2 -= res; d1 -= res; res += d1 / 2; res += d2; cout << res << '\n'; return 0; } ```
查看全文
1 0 2 2