题目传送门

一、理解题意

AcWing 148. 合并果子

二、算法思路

AcWing 148. 合并果子

三、实现代码

#include <bits/stdc++.h>

using namespace std;
//升序队列,小顶堆
priority_queue<int, vector<int>, greater<int>> q;

int res;

int main() {
    //优化输入
    ios::sync_with_stdio(false);
    int n;
    cin >> n;

    while (n--) {
        int x;
        cin >> x;
        q.push(x);
    }

    while (q.size() > 1) {
        int a = q.top();
        q.pop();
        int b = q.top();
        q.pop();
        res += a + b;
        q.push(a + b);
    }
    printf("%d\n", res);
    return 0;
}

相关文章:

  • 2021-12-26
  • 2021-12-16
  • 2022-01-09
  • 2021-11-06
  • 2021-07-10
  • 2021-07-19
  • 2021-09-01
猜你喜欢
  • 2021-10-18
  • 2021-09-08
  • 2022-12-23
  • 2021-07-05
相关资源
相似解决方案