首页 > 其他分享 >优先队列练习

优先队列练习

时间:2022-11-30 20:25:01浏览次数:56  
标签:优先 队列 top 练习 pop int ans push

题目链接:
https://www.luogu.com.cn/problem/P1090

#include <bits/stdc++.h>

using namespace std;

const int N = 1e4 + 10;
int a[N];

priority_queue <int, vector<int>, greater<int> > q;

int main(){
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		q.push(a[i]);
	}
	
	int ans = 0;
	while(q.size() > 1){
		int x = q.top();
		q.pop();
		int y = q.top();
		q.pop();
		ans += x + y;
		q.push(x + y);
	}
	
	cout << ans << endl;
	
	return 0;
}

标签:优先,队列,top,练习,pop,int,ans,push
From: https://www.cnblogs.com/csai-H/p/16939601.html

相关文章