文章

61

粉丝

98

获赞

17

访问

16.5k

头像
合并果子 题解:c++ priority_queue<int> 实现
P1544 中南大学机试题
发布于2024年3月23日 17:22
阅读数 150

#include<bits/stdc++.h>
using namespace std;


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

    cin >> n;
    for(int i = 0; i < n; i++){
        int tmp;
        cin >> tmp;
        pq.push(tmp);
    }

    int ans = 0;
    while(pq.size() != 1){
        int tmp = 0;
        tmp += pq.top();
        pq.pop();
        tmp += pq.top();
        pq.pop();
        pq.push(tmp);
        ans += tmp;
    }

    cout << ans << endl;


    return 0;
}
 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发