题解 | #游游的除2操作#
游游的除2操作
https://www.nowcoder.com/practice/b797f46aa75145a0bbe112099f7cbd18
考虑到数据范围,只有不超过 次的÷2操作,那么每次对非最小值÷2,然后排序,判断是否全为同一值即可。
#include <bits/stdc++.h> int main() { std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr); int n; std::cin >> n; int ans = 0; std::vector<int> a(n); for (int& i : a) std::cin >> i; std::sort(a.begin(), a.end()); while (true) { auto it = std::upper_bound(a.begin(), a.end(), a.front()); if (it == a.end()) { break; } int idx = it - a.begin(); for (int i = idx; i < n; i++) { a[i] /= 2; ans++; } std::sort(a.begin(), a.end()); } std::cout << ans << '\n'; }