P1090 合并果子(优先队列)
题目链接:https://www.luogu.com.cn/problem/P1090
#include <bits/stdc++.h> using namespace std; priority_queue<int, vector<int>, greater<int>> q; //小顶堆 int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int t; cin >> t; q.push(t); } int ans = 0 ; while (q.size() > 1) { int a = q.top(); q.pop(); int b = q.top(); q.pop(); ans += a + b; q.push(a + b); } cout << ans; }

浙公网安备 33010602011771号