1 条题解

  • 0
    @ 2025-4-21 11:32:20

    没想到数据结构班的内容这么难,跳过了基础直接上强度,比如这题,到底是让我学一个STL的使用还是去理解堆算法,迷惑迷惑,用自建堆算法我实现不了。

    #include #include using namespace std;

    int main() { int n; cin >> n; priority_queue<int, vector, greater> min_heap;

    for (int i = 0; i < n; ++i) {
        int length;
        cin >> length;
        min_heap.push(length);
    }
    
    long long total_cost = 0;
    while (min_heap.size() > 1) {
        int a = min_heap.top();
        min_heap.pop();
        int b = min_heap.top();
        min_heap.pop();
        
        int merged = a + b;
        total_cost += merged;
        min_heap.push(merged);
    }
    
    cout << total_cost << endl;
    return 0;
    

    }

    • 1

    信息

    ID
    2594
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    5
    已通过
    2
    上传者