模拟一下即可,每次取最大的元素
然后学到了c++map的访问最后一个元素(即最大的元素)用法
auto x = mp.end(); x --; // x->first 即为最大的元素值,x->second 即为该元素的个数 // 注意 x 是指针,所以访问要用 ->
#include <bits/stdc++.h> #include <queue> using namespace std; int main() { int n; cin >> n; int mn = 1e9 + 10; map<int, int> mp; for (int i = 0; i < n; i ++) { int x; cin >> x; mp[x] ++; mn = min(mn, x); } long long ans = 0; while (mp.size() > 1) { auto x = mp.end(); x --; mp.erase(x->first); ans += x->second; mp[x->first / 2] += x->second; } cout << ans; } // 64 位输出请用 printf("%lld")