1125 Chain the Ropes (25 分)【难度: 一般 / 贪心 哈夫曼树】

在这里插入图片描述
https://pintia.cn/problem-sets/994805342720868352/problems/994805350316752896
用小根堆即可。

#include<bits/stdc++.h>
using namespace std;
int n,x;
priority_queue<double,vector<double>,greater<double>>heap;
int main(void)
{
    
    
    int n; cin>>n;
    while(n--) cin>>x,heap.push(x);
    while(heap.size()>=2)
    {
    
    
        auto a=heap.top(); heap.pop();
        auto b=heap.top(); heap.pop();
        heap.push((a+b)/2);
    }
    printf("%d\n",(int)heap.top());
    return 0;
}

おすすめ

転載: blog.csdn.net/qq_46527915/article/details/121504753