解题思路:使用优先队列priority_queue;优先队列会按照队列中元素的优先权出列,
注意事项:默认定义priority_queue<int> que_name这样大者优先;priority_queue<int, vector<int>, greater<int> > que_name是小者优先;
大者优先,
参考代码:
#include <iostream> #include <queue> using namespace std; int main() { //int n; int count = 0; int h; cin>>n; priority_queue<int, vector<int>, greater<int> > que1; //这里有空格,不然会提醒错误,因为变成右移运算符<<了 for (int i = 0; i < n; ++i) { cin >> h; que1.push(h); } while (que1.size() != 1) { int x = que1.top(); que1.pop(); int y = que1.top(); que1.pop(); count += (x + y); que1.push(x + y); } cout << count; return 0; }
0.0分
0 人评分