开始 2024-08-19 00:00:00

8.19-8.24集训第一天

结束 2024-08-25 23:59:00
Contest is over.
当前 2025-04-20 03:10:49

I. 紧急集合-题解【满分AC】

众所周知,先献上【无注释满分AC代码】

#include<bits/stdc++.h>
using namespace std; 
priority_queue<int, vector<int>, greater<int> > q; 
int n, x, ans; 
int main(){
	cin >> n;
	for(int i = 1;i <= n;i++){
		cin >> x;
		q.push(x);
	}
	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;
}

思路:使用优先队列维护区间,每次找出最小的两个进行合并。


C_Fanhaoyu  •  8个月前

比赛已结束。