题解 | #游游的除2操作#

游游的除2操作

https://www.nowcoder.com/practice/b797f46aa75145a0bbe112099f7cbd18

考虑到数据范围,只有不超过 \log_2{\max{a_i}}次的÷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';
}

全部评论

相关推荐

02-19 16:50
已编辑
华南师范大学 Java
黑皮白袜臭脚体育生:春节刚过就开卷吗?哈基馆,你这家伙......
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务