本文主要是介绍NC16663合并果子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.优先队列
1.1 优先队列大根堆
#include<iostream>
#include<queue>
using namespace std;priority_queue<int> q;int main() {int n;cin >> n;for (int i = 1;i <= n;i++) {int tmp;cin >> tmp;q.push(-tmp);}int ans = 0;for (int i = 1;i <= n - 1;i++) {int x = q.top(); q.pop();int y = q.top(); q.pop();ans -= x + y;q.push(x + y);}cout << ans << endl;return 0;
}
1.2优先队列小根堆
#include<iostream>
#include<vector>
#include<queue>
using namespace std;priority_queue<int, vector<int>, greater<int>> q;//优先队列小根堆int main() {int n;cin >> n;for (int i = 1;i <= n;i++) {int tmp;cin >> tmp;q.push(tmp);}int ans = 0;for (int i = 1;i <= n - 1;i++) {int x = q.top(); q.pop();int y = q.top(); q.pop();ans += x + y;q.push(x + y);}cout << ans << endl;return 0;
}
这篇关于NC16663合并果子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!