【堆】【优先队列】[NOIP2004]合并果子
https://ac.nowcoder.com/acm/contest/22669/I
堆的用法
Type: 队列中存储元素的类型。例如 int,double,pair<int, int> 等。
Container: 底层存储数据的容器类型,默认为 vector
Compare: 比较函数,用于决定优先级顺序。默认是 less
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
priority_queue<int, vector<int>, greater<int>> q;
for(int i=0;i<n;i++) {
int t;
scanf("%d", &t);
q.push(t);
}
long long ans = 0;
while(q.size() > 1) {
int first = q.top();
q.pop();
int second = q.top();
q.pop();
long long total = first + second;
ans += total;
q.push(total);
}
cout << ans;
return 0;
}